HEX Code tabel
HEX codetabel
HEX codetabel
Hexadecimaal betekent letterlijk 16-tallig. Het is een talstelsel waarbij niet, zoals gebruikelijk, met tien cijfers wordt gewerkt, maar met zestien cijfers. De cijfers 0 t/m 9 worden daarom uitgebreid met ‘A’ (=10) t/m F (=15), ook wel ‘a’ t/m ‘f’. …
Website/bron
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
There is a possible complication when converting hexadecimal values to decimal. Lets start with the simple case, when the hexadecimal value represents an unsigned decimal value, and deal with the possibility of a signed number later. Hexadecimal numbers work the same way as the familiar decimal numbers, but there are 16 digits instead of 10, so each column goes up by a power of 16. For a four-digit number in base 10 we multiply the rightmost digit by 1, which is 10 raised to the power 0. Working towards the left, the next digit is multiplied by 10, or 10 raised to the power 1. The third digit is multiplied by 100, or 10 squared, and the final digit by 1000, or 10 cubed. Transferring that knowledge to base 16 numbers, the rightmost digit is still multiplied by 1, although technically it’s 16 raised to the power 0 this time. The second digit from the right is multiplied by 16, the third from the right by 256 (16*16), and the leftmost digit by 4096 (16*16*16). The additional piece of knowledge you need is the values for each of the hexadecimal digits. Digits 0 through 9 represent the same values as their decimal counterparts. For the digits 10-15, we use the letters A-F, so A is 10 and F is 15. Incidentally, using letters is becoming a standard for bases larger than 10. Some languages, like Ada, allow you to specify numbers in pretty much any base you like. For base 20 numbers, Ada uses A-J for the values 10-19. This convention allows you to use any base up to base 36. Getting back to your specific example, then, A432 is 10 * 4096 + 4 * 256 + 3 * 16 + 2 which works out to 42034 decimal. There is one possible gotcha. If the number you are converting is a hexadecimal representation of a number extracted from the bowels of a computer’s memory, you have to know if the number is signed or unsigned. For a four-digit unsigned hexadecimal value, the possible values of 0000 to FFFF convert to the decimal numbers 0 to 65535. That doesn’t allow for negative numbers, though. The shortest integer in most programming languages is still represented by four hexadecimal digits, but a special way of coding the numbers is used. It’s called two’s complement notation, and for a four-digit hexadecimal value you get decimal numbers from -32768 to 32767. Either way, there are 65536 possible values, but one way they are all positive or zero, and the other way half are negative. Here’s how two’s complement notation works: Any hexadecimal number with the most significant bit set is a negative number. As you probably know, each hexadecimal digit is actually four bits in the computer. The decimal, hexadecimal, and binary values for the 16 hexadecimal digits are: 0 0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5 0101 6 6 0110 7 7 0111 8 8 1000 9 9 1001 10 A 1010 11 B 1011 12 C 1100 13 D 1101 14 E 1110 15 F 1111 Any number from 0000 to 7FFF converts to decimal just like I described earlier. Numbers from 8000 to FFFF have the high bit set (which means the leftmost bit in the binary representation is one), so they are actually negative numbers. There are two ways to convert negative two’s compliment hexadecimal numbers to decimal. The first is the way the computer does it. To convert negative hexadecimal numbers to decimal, start by flipping all of the bits--each 0 becomes a 1, and each 1 becomes a 0. The result is the "complement" of the starting number. You’ll need a table to figure out what digits to substitute. You can work this table out for yourself from the one above, but I’ll save you the trouble. 0 F 1 E 2 D 3 C 4 B 5 A 6 9 7 8 Find the number you are complementing in the table, and replace it by the other number on the same line. A432 becomes 5BCD. The second step is to add one. 5BCD becomes 5BCE. Convert the resulting hexadecimal number to decimal, and put a minus sign in front. 5BCE is 23502 decimal, so the value A432 is -23502. The second method is to convert the original number to decimal, then subtract 65536, which is 2 raised to the power 16. Sixteen is also, you’ll notice, the number of bits in a four-digit hexadecimal number. A432 converts to decimal 42034, and 42034 - 65536 is, you guessed it, -23502. Ok, so which is it? is A432 really 42034 or -23502? The actual answer depends on how you are using the value. The hexadecimal value can represent either number; you have to know some other way whether you are using unsigned numbers, which means the decimal value is 42034, or two’s compliment numbers, in which case the decimal value is -23502. As an obscure aside, two’s compliment notation is really just a tricky way to handle negative numbers in a binary world that really deals only with positive values and zero. It’s a code, if you like. It’s far and away the most common code used to represent signed integers on modern digital computers, but it isn’t the only one. It’s been a long time since I used a computer that used something else, though, so unless you have some reason to believe your situation is really odd, it’s pretty safe to assume that any hexadecimal value that represents a signed decimal integer is using two’s compliment notation to do it. |
Website/bron
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
======================================== Converting to/from hexadecimal (base 16) ======================================== -Ian! D. Allen - idallen@idallen.ca - www.idallen.com The following link is a very good explanation and procedure for converting hexadecimal to decimal, including handling both unsigned numbers and two's complement negative numbers using bit flipping and adding one: http://www.madsci.org/posts/archives/2000-02/950277263.Cs.r.html Converting a negative two's complement number written in binary, octal, or hexadecimal, to decimal usually means flipping all the bits (doing a one's complement) and then adding one, converting the result to a positive number, and then putting a minus sign in front of the positive result. Since it's tedious to convert hex to binary, flip the bits, add one, then multiply it out to get decimal, here is a nice table that lets you "flip" (do a one's complement) on any hex digit directly: One's Complement (Bit-Flip) Table for Hexadecimal ------------------------------------------------- 0 <--> F ( 0000 <--> 1111 ) 1 <--> E ( 0001 <--> 1110 ) 2 <--> D ( 0010 <--> 1101 ) 3 <--> C ( 0011 <--> 1100 ) 4 <--> B ( 0100 <--> 1011 ) 5 <--> A ( 0101 <--> 1010 ) 6 <--> 9 ( 0110 <--> 1001 ) 7 <--> 8 ( 0111 <--> 1000 ) (You can create the above bit-flip table quickly by simply "folding" a list of 16 hex digits in half - write the hex digits down one side and then up the other side. The sum of any row is "F" [15].) Find the hex digit you are complementing (bit flipping) in the table, and replace it by the other number on the same line: 4A23 becomes B5DC. Once you've flipped the bits, you can complete the twos complement conversion process by adding one to the bit-flipped result. Another way to flip the bits is to subtract from -1, all bits on, e.g. FFFF - 4A23 = B5DC and FFFF - B5DC = 4A23 EXAMPLE 1: Convert two's complement 16-bit hex A2C9h to decimal. ---------- Step 1: Check the sign. If negative, make it positive and save the sign. A2C9 is negative (the sign bit is on, since "A" = 1010 binary) Use the table on A2C9 to flip the bits in each hex digit, giving 5D36 which is the one's complement, and then add one +1 giving 5D37 (a positive number, since "5" is 0101 binary) Step 2: multiply out the powers of 16 for each hex digit 5 times 16**3 = 5 times 4096 = 20480 D times 16**2 = 13 times 256 = 3328 3 times 16**1 = 3 times 16 = 48 7 times 16**0 = 7 times 1 = 7 Add them all up and get 23863 Step 3: put back the sign (saved from Step 1) Answer -23863 (negative) EXAMPLE 1B: Convert decimal -23863 to two's complement 16-bit hex. ----------- Step 1: Check the sign. If negative, make positive and remember to convert from positive hex to negative hex at the end. -23863 is negative. Save the sign and work on the positive number. Step 2: Convert +23863 (positive/unsigned) to hex by factoring out powers of sixteen. What are the powers of sixteen? 16^0 = (2^4)^0 = 2^0 = 1 16^1 = (2^4)^1 = 2^4 = 16 16^2 = (2^4)^2 = 2^8 = 256 16^3 = (2^4)^3 = 2^12 = 4096 16^4 = (2^4)^4 = 2^16 = 65536 65536 [16^4] is too big for 23863, so we know that we will have only four hex digits in our answer, from 16^3 down to 16^0. Subtract each successive power of 16: 23863 minus 5 times 4096 [16^3] equals 23863 - 20480 = 3383 3383 minus 13 times 256 [16^2] equals 3383 - 3328 = 55 55 minus 3 times 16 [16^1] equals 55 - 48 = 7 7 minus 7 times 1 [16^0] equals 7 - 7 = 0 --> DONE Step 3: Write down the powers of 16 as hexadecimal digits. 5 -> 5 times 16^3 A=10, B=11, C=12, D=13, E=14, F=15 13 -> D times 16^2 3 -> 3 times 16^1 7 -> 7 times 16^0 Step 4: Assemble the hex digits into a hexadecimal number, with the smallest powers of 16 on the right and the biggest (most significant) on the left. Remembering that the number was negative, Make the positive hex number into a negative hex number (twos complement) by complementing the bits (using the bit flip table) and adding one: 5D37 -> bit flip (using the hex bit flip table) -> A2C8 A2C8 plus one is A2C9 which is the answer. ANSWER: decimal -23863 converted to 16-bit two's complement hex is A2C9 EXAMPLE 2: Convert unsigned 16-bit hex A2C9 to decimal. ---------- Step 1: Unsigned numbers are all positive. No need to check any sign. Step 2: multiply out the powers of 16 for each hex digit A times 16**3 = 10 times 4096 = 40960 2 times 16**2 = 2 times 256 = 512 C times 16**1 = 12 times 16 = 192 9 times 16**0 = 9 times 1 = 9 Add them all up and get TOTAL 41673 Step 3: put back the sign (saved from Step 1) Unsigned numbers are always positive. Answer 41673 EXAMPLE 2B: Convert unsigned decimal 41673 to 16-bit hex. ----------- Step 1: Check the sign. Number is unsigned - no sign bit. No bit flip needed. Step 2: Convert 41673 (positive/unsigned) to hex by factoring out powers of sixteen. What are the powers of sixteen? 16^0 = (2^4)^0 = 2^0 = 1 16^1 = (2^4)^1 = 2^4 = 16 16^2 = (2^4)^2 = 2^8 = 256 16^3 = (2^4)^3 = 2^12 = 4096 16^4 = (2^4)^4 = 2^16 = 65536 65536 [16^4] is too big for 41673, so we know that we will have only four hex digits in our answer, from 16^3 down to 16^0. Subtract each successive power of 16: 41673 minus 10 times 4096 [16^3] equals 41673 - 40960 = 713 713 minus 2 times 256 [16^2] equals 713 - 512 = 201 201 minus 12 times 16 [16^1] equals 201 - 192 = 9 9 minus 9 times 1 [16^0] equals 9 - 9 = 0 --> DONE Step 3: Write down the powers of 16 as hexadecimal digits. 10 -> A times 16^3 A=10, B=11, C=12, D=13, E=14, F=15 2 -> 2 times 16^2 12 -> C times 16^1 9 -> 9 times 16^0 Step 4: Assemble the hex digits into a hexadecimal number, with the smallest powers of 16 on the right and the biggest (most significant) on the left. The number is positive/unsigned - no bit flipping is needed. ANSWER: decimal 41673 converted to 16-bit unsigned hex is A2C9 EXAMPLE 3: Convert two's complement 20-bit hex A2C9 to decimal. ---------- Step 1: Check the sign. If negative, make it positive and save the sign. A2C9h is actually 0A2C9h in hex, since we are told it is 20-bits wide. Because the sign bit is off (since "0" = 0000 binary), this is a positive 20-bit number. No need to flip any bits. Step 2: multiply out the powers of 16 for each hex digit 0 times 16**4 = 0 times 65536 = 0 A times 16**3 = 10 times 4096 = 40960 2 times 16**2 = 2 times 256 = 512 C times 16**1 = 12 times 16 = 192 9 times 16**0 = 9 times 1 = 9 Add them all up and get TOTAL 41673 Step 3: put back the sign (saved from Step 1) This is a positive number. Answer 41673 EXAMPLE 4: Convert two's complement 16-bit hex 1A8C to decimal. ---------- Step 1: Number 1A8Ch is positive, since the leftmost hex digit "1" is 0001 binary and we can see that the left (sign) bit is zero. Step 2: multiply out the powers of 16 for each hex digit 1 times 16**3 = 1 times 4096 = 4096 A times 16**2 = 10 times 256 = 2560 8 times 16**1 = 8 times 16 = 128 C times 16**0 = 13 times 1 = 13 Add them all up and get TOTAL 6796 Step 3: put back the sign (saved from Step 1) This is a positive number. Answer 6796 EXAMPLE 5: Convert two's complement 16-bit hex A123h to decimal. ---------- Step 1: Check the sign. If negative, make it positive and save the sign. A123h is negative (the sign bit is on, since "A" = 1010 binary) Use the table on A123 to flip the bits in each hex digit, giving 5EDC which is the one's complement, and then add one +1 giving 5EDD (a positive number, since "5" is 0101 binary) Step 2: multiply out the powers of 16 for each hex digit 5 times 16**3 = 5 times 4096 = 20480 E times 16**2 = 14 times 256 = 3584 D times 16**1 = 13 times 16 = 208 D times 16**0 = 13 times 1 = 13 Add them all up and get 24285 Step 3: put back the sign (saved from Step 1) Answer -24285 (negative) EXAMPLE 6: Convert unsigned 16-bit hex A123 to decimal. ---------- Step 1: Unsigned numbers are all positive. Step 2: multiply out the powers of 16 for each hex digit A times 16**3 = 10 times 4096 = 40960 1 times 16**2 = 1 times 256 = 256 2 times 16**1 = 2 times 16 = 32 3 times 16**0 = 3 times 1 = 3 Add them all up and get 41251 Step 3: put back the sign (saved from Step 1) Answer 41251 See Also -------- Any Base Converter: Convert numbers in any base up to 32: http://www.cut-the-knot.org/binary.shtml -- | Ian! D. Allen - idallen@idallen.ca - Ottawa, Ontario, Canada | Home Page: http://idallen.com/ Contact Improv: http://contactimprov.ca/ | College professor (Free/Libre GNU+Linux) at: http://teaching.idallen.com/ | Defend digital freedom: http://eff.org/ and have fun: http://fools.ca/ |