How Character Encoding Saved Me
Tags: character, string, encoding
Published: 2019-07-22 08:47:46

---
title: How Character Encoding Saved Me
published: true
description: How does a computer represent strings? why is it useful to know this?
tags: character,string,encoding
---

The _String_ data type is a very basic yet important data type every developer uses, but with its very common and everyday use, it is easy to neglect the internals of how it actually works. Many thanks partly to most of the new modern languages we have today, giving us _string data type_ on a platter of Gold.

However it is a very important thing to know the inner workings of most of the things we use, we can never tell how it might save us hours of code debugging one day.

So recently i was working on a project that involved cryptography using the *AES* encryption algorithm, and as part of the requirements of the system, i needed to double encrypt the data using two keys. Like any other developer would do, i went ahead did some googling, copied some code online 😅, pasted it and surely it worked, or so i thought

When it came to testing, i discovered, when i encrypted a string, and proceeded to decrypt it, i was getting back the data with some additional weird characters, this was causing my json serialisation code to break.

Now, the hard part 😪how do i remove the unwanted characters from my decryption method, it looked like a trivial problem, but took me 3days to wrap my mind around the problem.

Finally after 3 days of heavy googling, doubting my existence and life choices 😂, i stumbled on a piece of code from stackoverflow, 

``` CSharp
            StringBuilder sb = new StringBuilder(s.Length);
            foreach(char c in s)
            {
                if((int)c > 127) // you probably don't want 127 either
                    continue;
                if((int)c < 32)  // I bet you don't want control characters 
                    continue;
                sb.Append(c);
            }
            return sb.ToString();

```

It turned out, a simple knowledge of *Character Encoding* would have saved me hours of sleepless nights. What saved me was simple, check the characters that make up the string, any character that is less than 32, or greater than 127 according to ASCII, just discard. 

Every letter of the english alphabet and other languages are represented by bytes, over the years there has been different character encoding specifications, that has guided the representation of letters, UTF8,UTF16,UTF32,Unicode etc. A proper knowledge of the character encoding types and the different specifications, can allow you build better systems, especially in the area of languages.

### you can learn more from 
[What Every Programmer should know about string](https://hackernoon.com/what-every-programmer-should-know-about-string-a6611537f84e)

[The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)](https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/?source=post_page---------------------------)