Binary - Hex.. Hex - Binary - Decimal ... etc

Started by
4 comments, last by benryves 17 years, 6 months ago
I wanted to learn how to convert between. Binary, decimal and hexadecimal. Does anyone have a good internet resource for this? I've checked for a few on google but they don't tell me how to convert between each other. They just give me a few examples. Any help would be greatly appreciated.
Advertisement
Wiki
.
A good thing to remember is always:
Binary = base 2Decimal = base 10Hexadecimal = base 16

As such, you simply start counting until you reach the amount of numbers that equals the 'base number' and then start again. Let's look at how to do that in the three systems.
Binary: 0, 1, ( we wrote down to numbers now, so we add a '1' to the left ) 10, 11, 100, 101, 110, 111Decimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, (same as above) 10, 11, 12, 13 etc...Hexadecimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, (same as above), 10, 11, 12 etc..

That is counting and although you can deduct the converting from this I'll show you anyway.

To convert anything you have to write things as 'powers of'. That is, the hexadecimal number 0xFF3C (0x indicates it's hexadecimal) can also be written as:
F*16^3 + F*16^2 + 3*16^1 + C*16^0

As such, to convert 4018 to hexadecimal you search for the nearest 'power of 16'. 16^3 is 4096, so we are likely looking for a number that is to the power of two.
F*16^2 = 3840; 4018 - 3840 = 178. 178 / 16 = 11 (B)B*16^1 = 176; 178 - 176 = 22*16^0

Thus, FB2 is our number.

I'm afraid I don't have time to do this for binary as well, but it works the same way ;)
Oh.. great explanation. That helped a lot thank you.
Just a little trick on top of that, bytes consist of 8 bits (like we didn't know that :)), and are therefor often displayed as 8 binary numbers. The thing is, 4 binary numbers offer the same number of possible values than 1 hexadecimal number: 2^4 = 16. This is why bytes can easily be displayed as two hexadecimal values, and converting from binary to hexadecimal is as easy as translating each group of 4 bytes to their corresponding hexadecimal value.

So, for example, take the following binary value: 10110111. The last 4 bits, 0111, have a decimal value of 7. In hexadecimal, that's 7 too. The first four bits, 1011, have a decimal value of 11, which is B in hexadecimal. So, 10110111 is the same as B7.

A usefull thing to know when working with hex editors. Most of the time, you'll want to use some easier tools though. ;)
Create-ivity - a game development blog Mouseover for more information.
Four bits being called a nybble, of course. [smile]

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

This topic is closed to new replies.

Advertisement