HOME | WRITEUPS | TOOLS | RESOURCES | TUTORIALS | ABOUT |

Character codes are simple as each ASCII character having a number associated with it. They're incredibly useful because they allow us to represent a string numerically and reliably be able to get it back.

Each of the 128 ASCII characters has a character code, 0 to 127. As a reference, here's a list of all 128:

The main ones you will want to know are "0" is 48, "A" is 65, and "a" is 97. From there you'll be able to recognize the type of character a character code is for.

Let's say we have a string "PentaHex" and we want to be able to write it numerically in different bases (Don't know bases? See Number Bases).

Using the image above, we know that the character code for each of the letters in "PentaHex" is; 80 101 110 116 97 72 101 120. To get it in hexadecimal, we'll have to convert them manually or in a base converter. This gives us: 50 65 6e 74 61 48 65 78. We can also write it in binary: 01010000 01100101 01101110 01110100 01100001 01001000 01100101 01111000.

If we concatenate the hexadecimal numbers (50656e7461486578), it's equal to concatenating the binary numbers (0101000001100101011011100111010001100001010010000110010101111000). This works in base 4 too!

This is because each digit in base 2 is one bit (2^{1} = 2), each digit in base 4 is 2 bits (2^{2}) = 4, and each digit in base 16 is 4 bits (2^{4} = 16). As a result, 8 bits in binary is equal to into 8/2 = 4 digits in base 4 and 8/4 = 2 digits in base 16.

Let's onvert the number 474F5420495421 to a string.

Clearly, because of the F, this must be hexadeimal. We can split this number into groups of 2 digits, convert them to decimal, then use the table to convert these numbers to strings.

- First, let's split this number into groups: 47 4F 54 20 49 54 21.
- Next, individually convert each of these numbers to decimal: 71 79 84 32 73 84 33.
- Finally, go through the table and convert each number to it's corresponding character: "GOT IT!"

We're done! But... there's always an easier way! You can easily convert hexadecimal numbers to strings by utlizing programming languages.

Let's do the same problem, but use Python to decode it. To convert the hexadecimal number to a string, use the following code:

'474F5420495421'.decode('hex) # outputs 'GOT IT!'

Note that the number is stored a string before it is decoded.

Convert the array of numbers to a string: [80, 89, 84, 72, 48, 78, 95, 70, 55, 87]

For this example, let's use Python again. There's another built-in function for us to convert these easily!

chars = [80, 89, 84, 72, 48, 78, 95, 70, 55, 87]

converted = ""
for i in chars:

converted += chr (i)

print (converted)

This program goes through the list and does the chr function on each number, and adds it to the string in the converted variable. The chr function takes a character code (in decimal) and converts it to its corresponding string character.

The inverse of chr() is ord(). The ord function takes a string character and converts it to its corresponding character code.

Now that we know how character codes allow strings to be represented numerically, we might want to make use of this. Many encryption systems use numbers, so if we want to transmit strings, you just write it as a number with character codes! Often when decrypting a message, you are left with a number, usually hexadecimal. It's going to take a long time to convert every pair of digits into it's corresponding ASCII character.

You can use the ASCII / Base Converter You can convert between text and bases 2, 4, 8, 10, 16, and 64.