Thursday, May 8, 2014

HEX - The Basics


This blog post is just an explanation of Hexadecimal.

Hexadecimal or hex is a common way to view digital data and is directly related to the 1's and 0's that you always hear about when people discuss computers data.  Hex characters include:

0 1 2 3 4 5 6 7 8 9 A B C D E or F

Each Hex character represents four bits.  A bit is the smallest form of digital data and is represented by either a 0 or 1.  Think of it like a switch.  It is either off (0) or on (1).  You can think of a hard drive as an incredible number of microscopic switches.  So when you are viewing hex each character represents four bits.  Hex is any one of the 16 possible combinations that you can make from the variables of the four bits.  A group of four bits is called a nibble.  A group of two nibbles is called a byte.  Below you can see a diagram of how the 0 and 1 combinations can make one of the 16 possible hex characters.  Take some time to look this diagrams over to truly understand what is happening.  It's not complicated but if you aren't mathematically oriented (which I'm not) it takes a minute to ingest this.


So you can see that (if reading from left to right) the first bit is equivalent to an eight.  The second is worth equivalent to four, the third is equivalent to two, and the fourth is equivalent to 1.  The rest is basic arithmetic.  Please take a quick not that his is for Intel based processors and that it is possible for this to be reversed based on the architecture of the device but the majority of what we see is interpreted in this manner.

When viewing hex you will usually see the hex displayed in bytes with two nibbles next to one another and a space between each byte (pair of nibbles), like we see in this ghex screenshot:


As the user we usually encounter data with an extra layer of conversion (unless we are viewing the data with a hex editor).  Our interaction with a text file will help us understand this.  Go ahead and create a text file on your desktop (with notepad or gedit or whatever text editor you prefer.  Call it HEXTest.txt and type "Hello World!" in the file, then save it.  Now open that file with your hex editor.


We cans see our data and to the right we can see our plain text data.  So the byte 48 that is the first byte of our text file is interpreted by the text editor as the capital letter H.  The extra layer of interpretation that we are seeing is called ASCII or the American Standard Code for Information Interchange.  Essentially ASCII displays specific hex bytes into English characters and number.  Other software may interpret the hex in a different manner.  Below is a chart showing how hex characters are interpreted with ASCII.

.

So what happens when you add data to your text file at the hex level using your hex editor?  Lets find out. 


As you can see I've added a few zeros to the hex and also the word FAB.  Now lets open the again with the text error.


The hex editor initially stated that the file had "unsupported" characters and then displayed the hex plainly.  As you can see understanding what you are seeing in hex can greatly improve you understanding of the file.

More later.


No comments:

Post a Comment