How to convert Decimals into Binary then into Hex

This is where the Admins will put tutorials submitted by users.
Post Reply
VoYdE





Posts: 1474
Joined: Sun Aug 14, 2005 11:39 pm
Location: On Vacation From The Loads Of Bull*hit That Goes On Here
Contact:

How to convert Decimals into Binary then into Hex

Post by VoYdE »

Ok, This goes along with Cisco Networking.
And even though a calculator can do it in 0.00001 Seconds, it never hurts to learn.

Decimals are based off the power's of 2.
I.E
128 - 64 - 32 - 16 - 8 - 4 - 2 - 1
You stop at 128!

Its Really Quite Simple,

Heres How we go into Binary.
I give you the number 74.

Take your Powers.
128 - 64 - 32 - 16 - 8 - 4 - 2 - 1
And say can 128 (first number) fit into 74? NO So It Gets an "Off" = 0
so
128 = 0
64 = 1 (64 can fit into 74 so it gets a 1)
32 = 0 ( 64 + 32 = 96 (Over 74) so it gets an 0)
16 = 0 (64 + 16 = 80 (Over 74) so it also gets a 0)
8 = 1 (64 + 8 = 72 (Fits in) so it gets a 1)
4 = 0 (72 + 4 = 76 (to much) so it gets a 0)
2 = 1 (72 + 2 = 74 (Just right so it gets a 1)
1 = 0 (74 + 1 = 75 (to much) so it gets a 0)

So 74 is equal to 01001010 in binary.

Get it?

Now into Hex.
With the binary of 74 (01001010)
You Split it in half (2 groups of 4 characters) 0100-1010
In Hex you can only use 4 Digits of the powers of 2
So you can only use 8 - 4 - 2 - 1

0100-1010

0 (holds a 0 value so its nothing)
1 (holds a 4 value)
0 (holds nothing)
0 (holds nothing)
/
1 (holds an 8 value)
0 (hold a 0 value)
1 (holds a 2 value)
0 (holds a 0 value)

the first half is equal to 4 / the second half is equal to 10

so 0x4A

Oh and the A comes from
Hex is made by
0,1,2,3,4,5,6,7,8,9 AND
A = 10
B = 11
C = 12
D = 13
E = 14
F = 15
(it stops there, there is no G-Z)
So because the second half was equal to 10, The ten was changed to an "A"

Understand?

So 74 Is 01001010 In Binary And 0x4A In Hex.

This Is Used In Cisco! I.E Networking. The Values Only Go Upto 255 (Highest IP possible)

Its Fun (Class)!
Image
I made this 100% by myself, Everything from the background, to the location of the tags, Get yours http://customgamercards.com
User avatar
dos mes





Posts: 2158
Joined: Thu Dec 29, 2005 9:58 pm
Location: Syracuse, NY

Re: How to convert Decimals into Binary then into Hex

Post by dos mes »

Nice tut, my only question is, why do we have to add the numbers in bold? What is their significance?
VoYdE wrote: 64 = 1 (64 can fit into 74 so it gets a 1)
32 = 0 ( 64 + 32 = 96 (Over 74) so it gets an 0)
16 = 0 (64 + 16 = 80 (Over 74) so it also gets a 0)
8 = 1 (64 + 8 = 72 (Fits in) so it gets a 1)
4 = 0 (72 + 4 = 76 (to much) so it gets a 0)
2 = 1 (72 + 2 = 74 (Just right so it gets a 1)
1 = 0 (74 + 1 = 75 (to much) so it gets a 0)
VoYdE





Posts: 1474
Joined: Sun Aug 14, 2005 11:39 pm
Location: On Vacation From The Loads Of Bull*hit That Goes On Here
Contact:

Post by VoYdE »

No i just spread it all out.

you want to add up everything to GET 74, you cant go over or under 74 you have to be dead on.

so in this
128 = 0 (128 cant fit into 74 so it gets a 0)
64 = 1 (64 can fit into 74 so it gets a 1)
32 = 0 ( 64 + 32 = 96 (Over 74) so it gets an 0)
16 = 0 (64 + 16 = 80 (Over 74) so it also gets a 0)
8 = 1 (64 + 8 = 72 (Fits in) so it gets a 1)
4 = 0 (72 + 4 = 76 (to much) so it gets a 0)
2 = 1 (72 + 2 = 74 (Just right so it gets a 1)
1 = 0 (74 + 1 = 75 (to much) so it gets a 0)

If you add anything that has ((number) = 1) it will equal 74
so...

64+8+2=74
Anything with a 0 is an (off) so you dont count it.
Image
I made this 100% by myself, Everything from the background, to the location of the tags, Get yours http://customgamercards.com
User avatar
[cc]z@nd!




Literarian 500

Posts: 2297
Joined: Tue May 04, 2004 1:52 pm
Location: michigan

Post by [cc]z@nd! »

here's how i do it by hand in networking. also, my friend made a nifty program using a bunch of if statements that any TI-83 related calculator can run to convert n 8-bit decimal into it's binary equivalent.


anyways, here's my easy way of doing it by hand that i use:

00101101 = ? //we don't know what 00101101 is in decimal, so we're going to find out!

because of previously discussed value places in binary (1,2,4,8,16,32,64,128, etc.) we can do this

00101101

skip 128 because it's a zero ( 0 )
skip 64 because it's a zero ( 0 + 0 = 0 )
add 32 because it's a one ( 0 + 0 + 32 = 32 )
skip 16 ( 0 + 0 + 32 + 0 = 32 )
add 8 ( 0 + 0 + 32 + 0 + 8 = 40 )
add 4 ( 0 + 0 + 32 + 0 + 8 + 4 = 44 )
skip 2 ( 0 + 0 + 32 + 0 + 8 + 4 + 0 = 44 )
add one ( 0 + 0 + 32 + 0 + 8 + 4 + 0 + 1 = 45 )
simplify ( 32 + 8 + 4 + 1 = 45 )

45.


and then when i need to put a decimal into binary, say 156, here's what i do.

our decimal number (156) we can represent with D. here's what will happen:

IF D > selected_bit_value, selected_bit = 1
LOOP! :D

ok, so that's sorta program-cryptic, but it basically checks if variable D is bigger then variable selected_bit_value, and if it is, it sets the variable selected_bit to 1. 156 is bigger than 128, so we set the first bit in our byte to 1. so far we have 1xxxxxxxx. to continue on correctly, do this

D = D - selected_bit_value. // sets the variable D to the value of D - selected_bit_value

156
-128
28 (tell me if my math's wrong, i did this in my head staring at the monitor :P)
so we can continue going through that above process like this (if a mathematical sign has a ! before it, it means not. so "not equal to" looks like this: != )

156 > 128, 156 - 128 = 28, 1
28 !> 64, 28 - 64 !> 0, 0
28 !> 32, 28 - 32 !> 0, 0
28 > 16, 28 - 16 = 12, 1
12 > 8, 12 - 8 = 4, 1
4 = 4, 4 - 4 = 0, 1
0 !> 2, 0 - 2 !> 0, 0
0 !> 1, 0 - 1 !> 0, 0

so our binary equivalent of 156 is 10011100. now hex! this is easy. the way i go through this, is i keep a hex table memorized .. sorta.

note: a total of 16 values including null (0). also, pay attention to the pattern binary counts in, it gets real useful, real fast.


0 - 0000 - 0
1 - 0001 - 1
2 - 0010 - 2
3 - 0011 - 3
4 - 0100 - 4
5 - 0101 - 5
6 - 0110 - 6
7 - 0111 - 7
8 - 1000 - 8
9 - 1001 - 9
10 - 1010 - A
11 - 1011 - B
12 - 1100 - C
13 - 1101 - D
14 - 1110 - E
15 - 1111 - F

what people have problems with is the fact that they're too used to counting by ten digits. all of these are legitamete ways to count, otherwise they wouldn't be used. just think of it as 2 digits, or 16 digits to go into one place.

sorry if it's lengthy, but a second perspective can really help me understand other things, like pointers in C languages. i couldn't get it the way to book explained it, but i read through slwly, and related it to road signs.
ASPARTAME: in your diet soda and artificial sweeteners. also, it's obviously completely safe. it's not like it will cause tumors or anything. >.>
always remember: guilty until proven innocent
Post Reply