   Hex dumps
  Hexdumps are mentioned in Chapter 15.1. What are hexdumps2. What are the purposes of a hexdump?   
  A hex dump is a translation of binary data into a series of hexadecimal numbers that represent that data. For example a text file that contained "hello world" (in ASCII) might be dumped like:68 65 6c 6c 6f 20 77 6f 72 6c 64;Quote:Original post by SiCraneA hex dump is a translation of binary data into a series of hexadecimal numbers that represent that data. For example a text file that contained "hello world" (in ASCII) might be dumped like:68 65 6c 6c 6f 20 77 6f 72 6c 64Ok but what is the point of doing so - what do you get out of a hex dump that is valuable?   ;  It's a relatively compact, but human readable way to determine contents of files or of memory. Let's say you're writing an application to save some data to a file in binary format, and you can't seem to get reading to back to work. Maybe your reading code is bad or maybe your writing code is bad. One way to inspect the file is to get a hex dump and see if the right values are in the right places.   ;Quote:Original post by SiCraneIt's a relatively compact, but human readable way to determine contents of files or of memory. Let's say you're writing an application to save some data to a file in binary format, and you can't seem to get reading to back to work. Maybe your reading code is bad or maybe your writing code is bad. One way to inspect the file is to get a hex dump and see if the right values are in the right places.One handy feature of hex is that each digit represents exactly 4 bit. A pair of them then equal one byte. That's why SiCrane grouped them into two's. So just by looking over the hex string he posted as an example, you can tell which bits each digit corresponds to. If I want to know the values of bits 16-19, I just have convert the 5th digit to binary. (first digit represents bits 0-3, second 4-7, third 8-11 and so on)   ;Quote:Original post by SpoonbenderIf I want to know the values of bits 16-19, I just have convert the 5th digit to binary. (first digit represents bits 0-3, second 4-7, third 8-11 and so on)Sorry I'm not sure I follow what you are saying here. Could you give a more detailed explanation please?;  Well, the thing about hex is that each digit can be mapped directly to the underlying bit pattern.A hex digit has one of 16 values. That's 2^4, so to represent one hex digit, we need exactly 4 bits, and every time we have 4 bits, they can be represented by exactly one hex digit.And since a byte is 8 bits, we know that each byte can always be represented by two hex digits.So, let's imagine we're debugging your game. Perhaps the data in question is a network packet, and for convenience, let's use SiCrane's example data:68656c6c6f20776f726c64(I'm removing the spaces because we're lazy while debugging, and don't want to waste time inserting spaces, we just output the data directly)Now, we know exactly which bits are covered by each digit. If I want to look at the 6th byte, I just have to read digits 12 and 13 (which contain 2 and 0 in this case). If I just want to look at the 4 most significant bits of the 6th byte, I only need to look at digit 12.And if I want to write them out as binary, I only have to consider one digit at a time. The 6th byte, which contains 0x20 would then be converted like this.2: 00100: 0000So byte 6 as a whole: 00100000It doesn't matter where a hex digit occurs, it always represents the same bit pattern. 2 is always 0020, 0 is always 0000. So if the byte we were looking at had read 0x02 instead of 0x20, we'd get 00000010 instead.If I'd used decimal instead, there wouldn't be such a direct mapping between each digit and the underlying bits.For example, the decimal value 9 has the binary representation 1001. But 90 looks like this: 1011010.What is the 10's doing messing around all the way down at the second-lowest bit? Shouldn't it stick to manipulating the higher bits? And why doesn't the pattern for 9 (1001) appear anywhere?Where's the logic in this? I have to translate the entire number, the individual digits don't tell me anything useful. Even if I'm only interested in the most significant bits of the byte with value 90, I still have to consider the least significant digit (the 0) as well. I can't just concentrate on the 9.And conversely, if you have to convert a bit sequence, it's also a lot easier in hex.Let's say you're setting a bunch of flags (so 1 bit each), and for convenience, we have 8 flags (so 1 byte overall)Now I decide that I want the flags set as follows:00101110 (which might be the settings for "invincibility, infinite ammo, start on level 3, or whatever you can think of)The problem is, when you try to set these flags in code (or in the debugger), you can only enter the byte value as hex or decimal, not as a bit pattern. So you have to translate it.Translating to hex is easy. Just take 4 bits at a time:0010 = 0*8 + 0*4 + 1*2 + 0*1 -> 21110 = 1*8 + 1*4 + 1*2 + 0*1 -> 14 in decimal (and in hex, 10 is a, 11 is b, and so on, so 14 must be an e) -> eSo you just type '2e' into your debugger, and voila, you have infinite ammo and are on level 3. Just what you needed to track down this bug.In decimal:We have to take the whole sequence in one go. So0*128 + 0*64 + 1*32 + 0*16 + 1*8 + 1*4 + 1*2 + 0*1 = ummm.... 46! But this time we had to add up a lot of bigger numbers, which aren't as easy to do in your head. (most people know what 8 + 4 is, but have to stop and think if you ask them for 128 + 64)That's why hex is convenient. Much nicer to work with than the messy decimal system [grin]   ;Quote:Original post by Spoonbender(I'm removing the spaces because we're lazy while debugging, and don't want to waste time inserting spaces, we just output the data directly)Bah. Your laziness is an inferior quality laziness. True laziness would be to use an existing hex dump program and not write code for it. In the case of a network packet you can grab a packet sniffer like WireShark that will display the spaces.   ;Quote:Original post by SiCraneQuote:Original post by Spoonbender(I'm removing the spaces because we're lazy while debugging, and don't want to waste time inserting spaces, we just output the data directly)Bah. Your laziness is an inferior quality laziness. True laziness would be to use an existing hex dump program and not write code for it. In the case of a network packet you can grab a packet sniffer like WireShark that will display the spaces.But that would have totally ruined my point! [lol]How was I supposed to show that hex is nice and convenient to navigate through, if I insert artificial spacing to make it nicer and more convenient to navigate through? [grin]People expect examples to be contrived... Can't break with tradition.   ;  Thanks Spoonbender, that was a great explanation.