Ok,so I know that the binary equivalent of 104 is 1101000.
10=1010
4=0100
You can't break apart a number like 104
into 10
and 4
when changing bases. You need to look at the number 104
in its entirety. Start with a table of bit positions and their decimal equivalents:
1 1
2 10
4 100
8 1000
16 10000
32 100000
64 1000000
128 10000000
Look up the largest decimal number that is still smaller than your input
number: 104
-- it is 64
. Write that down:
1000000
Subtract 64
from 104
: 104-64=40
. Repeat the table lookup with 40
(32
in this case), and write down the corresponding bit pattern below the first one -- aligning the lowest-bit on the furthest right:
1000000
100000
Repeat with 40-32=8
:
1000000
100000
1000
Since there's nothing left over after the 8
, you're finished here. Sum those three numbers:
1101000
That's the binary representation of 104
.
To convert 1101000
into hexadecimal we can use a little trick, very similar to your attempt to use 10
and 4
, to build the hex version from the binary version without much work -- look at groups of four bits at a time. This trick works because four bits of base 2
representation completely represent the range of options of base 16
representations:
Bin Dec Hex
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 10 A
1011 11 B
1100 12 C
1101 13 D
1110 14 E
1111 15 F
The first group of four bits, (insert enough leading 0
to pad it to four
bits) 0110
is 6
decimal, 6
hex; the second group of four bits, 1000
is
8
decimal, 8
hexadecimal, so 0x68
is the hex representation of 104
.