# Converting binary, decimals, hexadecimals, octets



## GSquadron (Mar 22, 2010)

Hello guys!
I got an exam for tomorrow so i am in need of your help (i dont have the book about this so...)
Now lets take for example a binary number: 01101110(2)=?(16)
or from binary to decimal: 01101110(2)=?(10)
or from binary to octets: 01101110(2)=?(8)
The same thing is for all other convertings, just that instead of binary you will put oct, hex and decimal numbers.
How to do these convertings?
Pls explain them and give the examples


----------



## Mussels (Mar 22, 2010)

....


AAAAAararaaAAAAAAARRGGGGGGHHHHHHHHHH


i still have nightmares about learning this stuff - and its never proven useful to me in the real world.


----------



## GSquadron (Mar 22, 2010)

Well it is not such a nightmare if you guys help me PLS!!!!


----------



## FordGT90Concept (Mar 22, 2010)

01101110 is an octet because there is 8 binary values.  It depends if the values are read left to right or right to left.

Left to right:
0 = 1 * 0 = 0
1 = 2 * 1 = 2
1 = 4 * 1 = 4
0 = 8 * 0 = 0
1 = 16 * 1 = 16
1 = 32 * 1 = 32
1 = 64 * 1 = 64
0 = 128 * 0 = 0

0 + 2 + 4 + 0 + 16 + 32 + 64 + 0 = 118 decimal = 76 hex

Right to left:
0 = 1 * 0 = 0
1 = 2 * 1 = 2
1 = 4 * 1 = 4
1 = 8 * 1 = 8
0 = 16 * 0 = 0
1 = 32 * 1 = 32
1 = 64 * 1 = 64
0 = 128 * 0 = 0

0 + 2 + 4 + 8 + 0 + 32 + 64 + 0 = 110 decimal = 6E hex


10 and 16 have the same value unless the significant digits have changed.


Hex is simply:
0 = 0
1 = 1
2 = 2
3 = 3
4 = 4
5 = 5
6 = 6
7 = 7
8 = 8
9 = 9
A = 10
B = 11
C = 12
D = 13
E = 14
F = 15

To convert decimal to hex:
Divide by 16: 110 / 16 = 6.875
Take the floor (round down to the nearest whole number); this is your first digit: 6
Bultiple the first digit by 16: 6 * 16 = 96
Subtract that result from your original value: 110 - 96 = 14
Convert all digits to hex:
6 = 6
14 = E
The result is 6E.


Hex to decimal is much quicker:
16^position * value += sum

Note: if position is zero, exclude that calculation.

For 6E:
16^1 * 6 +  E = sum

Convert E to its decimal value of 14:
16^1 * 6 +  14 = sum

Solve:
16 * 6 + 14 = sum
96 + 14 = sum
110 = sum


Octets are useless.  Binary, decimal, and hexadecimal are useful in programming.


----------



## GSquadron (Mar 22, 2010)

Ok, but what about decimals to octets?


----------



## FordGT90Concept (Mar 22, 2010)

It's the same as hex but instead of 0-F, it is 0-7.  Instead of 16, use 8.  Decimal is the odd ball.  Binary, octets and hexadecimal get along fine.


----------



## GSquadron (Mar 22, 2010)

How do i convert, for example:530 to octet? Pls explain, i dont get it


----------



## Thrackan (Mar 22, 2010)

I always use Google for stuff like that


----------



## GSquadron (Mar 22, 2010)

Yeah i have searched there, but there is a stupid explanation, as people usually try to write against others and i dont really know which article is the right one


----------



## Thrackan (Mar 22, 2010)

Oh, I didn't mean that I use Google to find out how to convert these things, I use Google to convert them *for* me


----------



## mlee49 (Mar 22, 2010)

Ok I just learned this and tested on this this semester.  Here goes:

First off define your Base counting system: 

Binary uses 1's and 0's so it's a BASE 2 counting system.  
Decimal uses digits 0-9, so it's a BASE 10 counting system.
Octal and HEX are 8 and 16 respectively based on the same logic.

2nd Define your columns.  

Since Binary can only use 0's and 1's to count it uses more columns to create larger numbers.  The right-most number is column *ZERO*. Progressing right to left counting upwards.
Example:  (1010)  the right most 0 is in column 0. The right most 1 is in column 1, the green zero is in column 2 and finally the blue 1 is in column 3.


Converting:

It's very simple once you get the hang of it, just remember which BASE your going *from and to*

Binary to Decimal: 
Goes from a BASE 2 system to a BASE 10 counting system.  The trick is simple and will be useful in future base conversions, an example will do best:

(1010) in BASE 2 converted into a BASE 10 will be: (1x2^3)+(0x2^2)+(1x2^1)+(0x2^0)=10

Again:
(1111) base 2 is (1x2^3)+(1x2^2)+(1x2^1)+(1x2^0)= 15

Again:
(1001) base 2 is (1x2^3)+(1x2^0)=9

Decimal to Binary is simple as well:

Simply divide the decimal number by the BASE counting system's number and you will have a Quotient and remainder.  Keeping track of the remainder will give you your converted binary number.

Example:
10 in base 10 to base 2:

10/2=  5    remainder 0
5/2  =  2   remainder 1
2/2  =  1   remainder 0
1/2 =   0.5  remainder 1   STOP since we have reached below 0.

Line up the remainders in order, (1010) which is 10.

Example:
15 in BASE 10 to BASE 2:

15/2 =  7  remainder 1
7/2 = 3  remainder 1
3/2 = 1 remainder 1
1/2 =0.5 remainder 1  STOP since we have reached below 0.

Line up the remainders: (1111)  which is 15 in base 2.


This logic can be applied to all base conversion's so BASE 8 to BASE 10:

(234) in base 8 to base 10= (2x8^2)+(3x8^1)+(4x8^0)= 156 BASE 10

Recognize the patterns and apply it to the same type of problem and you can understand the logic behind this.


Also, there are tons of good videos on youtube that will help.


----------



## epicfail (Mar 22, 2010)

this is what i used for that class, http://www.tonymarston.net/php-mysql/converter.php 

edit: reread what you said oops, ya i dont really know how to explain it i just used the calc for everything


----------



## GSquadron (Mar 22, 2010)

DEC/HEX/OCT - BIN
0 -0000                                            0-0000        0-000
1 -0001                                               1-0001 1-001
2 -0010                                          2-0010 2-010
3 -0011                                        3-0011 3-011
4 -0100                                          4-0100 4-100
5 -0101                                     5-0101 5-101
6 -0110                                        6-0110 6-110
7 -0111                                                 7-0111 7-111
8 -1000                                                           8-1000
9 -1001                                                           9-1001
10-1010                                                           A-1010
11-1011                                                           B-1011
12-1100                                                           C-1100
13-1101                                                           D-1101
14-1110                                                           E-1110
15-1111                                                           F-1111
Simplier than this it can't be! Just use the numbers in the table to convert


----------

