Contributors:
jimblom Converting To/From DecimalBy now, we know how to convert about 16-or-so values between decimal and hexadecimal. To convert bigger numbers, here are some tricks we use.
Converting Decimal to HexConverting from decimal to hex involves a lot of division and remainders. If you've pushed long division out of your brain, wiki's always there to help you brush up.
The steps to convert a number, let's call it N, from decimal to hex look something like this:
Enough math-speak, let's work an example. Let's convert 6145310 to hexadecimal:
Finally, combine all four hex digits to create our hex value: 0xF00D.
Converting Hex to DecimalThere's an ugly equation that rules over hex-to-decimal conversion:
There are a few important elements to this equation. Each of the h factors (hn, hn-1) is a single digit of the hex value. If our hex value is 0xF00D, for example, h0 is D, h1 and h2 are 0, and h3 is F.
Powers of 16 are a critical part of hexadecimal. More-signficant digits (those towards the left side of the number) are multiplied by larger powers of 16. The least-significant digit, h0, is multiplied by 160 (1). If a hex value is four digits long, the most-significant digit is multiplied by 163, or 4096.
To convert a hexadecimal number to decimal, you need to plug in values for each of the h factors in the equation above. Then multiply each digit by its respective power of 16, and add each product up. Our step-by-step approach is:
Here's an example of a four-digit hexadecimal value, 0xC0DE, that we want to convert to decimal. Creating a table and sorting the digits into separate columns can make the conversion process easier:
Hexadecimal Digit Notes Digit Positions (n) 3 2 1 0 These values are statically assigned, they grow to the left. Hex Digits Sorted C 0 D E This part's easy, plug your hex values in from right-to-left. Convert A-F 12 0 13 14 Convert hex values A-F to 10-15. Multiply by 16n 12 × 163 0 × 162 13 × 161 14 × 160 The exponent of 16 is the position, n. Resulting Products 49152 0 208 14 The product of hex digit and the power of 16. Sum Up All Products 49374 Our decimal equivalent!There you have it. CODE16 = 4937410!
This table method is perfect for keeping all of your hex digits, positions, and powers-of-16 in line. To convert larger hex numbers, just add a columns to the left and increase n.
Now that you know how to do it by hand, save yourself a little time and use a calculator.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4