home
copy-paste.md

Copy and Pasting is Hard

2022-02-11

I had to do some text conversions in C and not being particularly good at C or having an understanding of how to convert iso8859 to utf8 I simply googled for the answer. Luckily someone had already done it for me and so it was a straight copy and paste for the most part. The code itself makes sense and it looks straightforward. The trouble is that you rarely can just copy and paste, usually you need to make some tweaks. I also made those tweaks and ran into a strange issue where I lost a letter. After some debugging I found that there was a pointer getting incremented when it shouldn’t be. This was after hours of screwing around with code as I didn’t know where in the process the character was getting lost or that there was even a character being lost. I was printing values all over the place! I ultimately narrowed it down to a bit of code that I copied over and while renaming things to be more explicit, I added a ++ to a pointer that I shouldn’t have. I then went back to where I took the logic from and it was as plain as day. I am an idiot. I thought maybe the stackoverflow answer was wrong but nope, I played myself.

There’s a lesson here. I should try to read each line of code and internalize it. Just making modifications where I need to isn’t enough.

In other news, at some point I want to write my own converter and I think that could be a fun project to switch text from one encoding to another.