# Hex and Binary

I intend to build up this guide into a series of more complex “lessons” so eventually we can read packets as they are on the wire and you will be able to interpret what you are seeing without too much difficulty.

Time to start with the basics.  In order to understand and read packets, we need to know the fundamentals.  How do computers and network communicate?  Essentially by binary and hexadecimal.  This is a series of zero’s and one’s and the numbers 0 to 9 with the letters A to F.

When I was taught this in school, some 20ish years ago, I honestly found it a bit complicated, but looking back it was only complicated due to the way it was taught.  Hopefully this methodolgy is simple for you to understand.

Counting in binary is not too difficult, the values can only be a 0 or a 1, an off or on value.  However what the off or on values represent is the important ‘bit’.

You essentially have 8 bits in a byte and this makes binary reasonably straight forward, for counting I find it best to create a quick table, this allows me to visually count, rarther than attempting to work out everything in my head. What the chart shows, we have the 8 bits represented across the top.  The numbers 128 down to 1.  The base 10 is the representation of the value in decimal, the numbers we are familar with.  The values 128 to 1 are powers of 2.

We have the following
20 = 1
21 = 2
22 =4
23 =8
24 = 16

This is essentially doubling up each time, and this is how binary works and is pretty straight forward.

In my example above, we have a a value of 1 in the columns that represent 128, 64, 8 and 4.  So all we do now is add these up.

128+64+8+4 = 204
128+32+4+4 = 172
32+8+2+1=43
128+64+32+16+8+4+2+1=255

For me this chart makes it easy, whenever I am required to convert binary into decimal, I always create the chart on a bit of scrap, fill in the relevant fields and add them up.

Now you can work out Hexadecimal in a similar way, which I find is far easier for my poor brain to understand.  When I was taught hex in school, I was taught to convert the hex to binary and then into decimal, which you can do, however it creates an extra step, which takes longer and there is one extra step to make a mistake.

Base16
0 to F
Seems complicated? Not really, its as easy as binary.
16­0 = 1 = 20
161 = 16 = 24
162 = 256 = 28
163 = 4096 = 212 This simple hex chart covers what you need to know.  Starts from 0 to 9, decimal 10 to 15 is represented with A to F.

So how do we calculate hex values? I will show you the same methodolgy that I use for binary conversion.  A nice and simple chart. So what does this mean exactly?  Well we do a similar method to binary.

We have 0x20 which is how we represent hex, when you see it in this format, it us telling you this is a hexadecimal value.

0x20 = (2 x 16) + (0 x 1) = 32 + 0 = 32 decimal
0x203 = (2 x 256) + (0 x 16) + (3 x 1) = 512 + 0 + 3 = 515 decimal
0x378 = (3 x 256) + (7 x 16) + (8 x 1) = 768 + 112 + 8 = 888 decimal
0xBAF = (11 x 256) + (10 x 16) + (15 x 1) = 2816 + 160 + 15 = 2991 decimal

So in the first example, we have the hex value of 20.  So these fill in the 2 colums on the right, 2 in the 16 value and 0 in the 1 value.  To calculate we just multiple 2 by 16, so we have 32. 0 multiplied by 1 is 0, so the total value in decimal is 32.

The second example, we have 0x203, so using the same formula we have to multiply 2 by 256, multiply 0 by 16 and multiply 3 by 1, and we then just add these figures up giving us the total of 515 decimal.

Simples.

## 3 thoughts on “Hex and Binary”

1. r3dux says:

Like it!

You should slap it so anyone can comment not just registered users, otherwise there’ll be not many comments.

Chat soon, bro =D

1. Treznor says:

Yeah I need to find something to stop the spam, I dont want all those bots to just fill comments with drug spam.

How are things with you? We need to catch up sometime!

1. r3dux says:

Good! Busy! PhD’s a massive time vampire – but I did finally write a software library as one of my papers:

Took me like 18 months to get that thing working properly… just crazy.

Looks like things are going well for you – which is fantastic. Ping me an email some and we can chat =D

This site uses Akismet to reduce spam. Learn how your comment data is processed.
{"ft_lang_label":"__label__en","ft_lang_prob":0.9301185,"math_prob":0.973847,"size":4184,"snap":"2023-14-2023-23","text_gpt3_token_len":1168,"char_repetition_ratio":0.10191388,"word_repetition_ratio":0.0,"special_character_ratio":0.31022945,"punctuation_ratio":0.09411765,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99617255,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12],"im_url_duplicate_count":[null,2,null,2,null,2,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-06-10T01:08:23Z\",\"WARC-Record-ID\":\"<urn:uuid:5bc26196-d76a-42e5-9ead-742b68c863b1>\",\"Content-Length\":\"59657\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:88f7db1c-5be8-4e5a-bc10-50888df890fc>\",\"WARC-Concurrent-To\":\"<urn:uuid:9f80c8f6-05a8-4667-9aaa-2a4c9fa51734>\",\"WARC-IP-Address\":\"68.171.223.227\",\"WARC-Target-URI\":\"https://truxnor.net/index.php/2015/12/30/hex-and-binary/\",\"WARC-Payload-Digest\":\"sha1:JKWIKXO5BZHIM2JQRH2AAYKN5I6VVQSV\",\"WARC-Block-Digest\":\"sha1:PXJE4Y63WX4VCRN6CASUJFAPT47FSZ64\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224656869.87_warc_CC-MAIN-20230609233952-20230610023952-00744.warc.gz\"}"}