2

Ok,so I know that the binary equivalent of 104 is 1101000.

10=1010

4=0100

so , 104=1101000 (how to get this??how these both mix together and get this binary?)

And from the example here...

the octets from "hellohello" are E8 32 9B FD 46 97 D9 EC 37.

This bit is inserted to the left which yields 1 + 1101000 = 11101000 ("E8").

I still understand this part , but how to convert 11101000 to E8?

I'm so sorry for all these noob questions , I just learn it yesterday , I googled and search for a whole day but still not really understand the concept...

Thank you.

Irene Ling
  • 1,881
  • 3
  • 26
  • 41

4 Answers4

5

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.

sarnold
  • 102,305
  • 22
  • 181
  • 238
  • Thanks buddy :) Once I get a new pile of votes tomorrow, expect a +1. Bug me if I appear to have forgotten. :) – sarnold Nov 13 '11 at 08:33
  • What a neat and clear table , I finally understand how it works now.Thank you so much sarnold, guess I will keep this table forever lol. – Irene Ling Nov 13 '11 at 09:59
2

I think you are making some confusions: 104 decimal is 1101000 which is not formed by two groups splitting 104 into 10 and 4. The exception is for hex numbers that can be formed by two groups 4 binary numbers (2^4 = 16).

So 111010000 = E8 translates into 1110 = E and 8 = 10000. 1110 (binary) would be 14 (decimal) and equivalent of E (hex). Hex numbers go from 0 to 15 (decimal) where:

  • 10 (decimal) = A (hex)
  • 11(decimal) = B(hex)
  • ...
  • 15(decimal) = F(hex)
misha
  • 2,760
  • 3
  • 28
  • 30
  • There used to be a BCD (Binary Coded Decimal) encoding, which encoded every individual decimal digit with its binary equivalent. So 104 would have been encoded as 0001 0000 0100. I don't think I saw this being used the last 30 years or so. – Sjoerd C. de Vries Nov 13 '11 at 08:13
  • BCD is still commonly used on calculators, because it is very easy to give "decimal exact" answers that don't suffer from [some silly behavior common to floating point numbers when used for every day consumer problems](http://stackoverflow.com/questions/752738/why-is-the-result-of-this-explicit-cast-different-from-the-implicit-one/752755#752755). The HP48, HP49, HP50, HP38, etc. all use BCD internally. I would not be surprised if TI did the same for their models. – sarnold Nov 13 '11 at 08:20
  • Right, I remember a "use" of it from school - displaying numbers using 7 segment display (digital clock). – misha Nov 13 '11 at 08:21
  • Yes I confused with that but I understand now , thank you so much misha. – Irene Ling Nov 13 '11 at 10:00
2

What you're missing here is the general formula for digital numbers.

104 = 1*10^2 + 0*10^1 + 4*10^0

Similarly,

0100b = 0*2^3 + 1*2^2 + 0*2^1 + 0*0^0

And for a hexidecimal number, the letters A-F stand for the numbers 10-15. So,

E8 = 14*16^1 + 8*16^0

As you go from right to left, each digit represents the coefficient of the next higher power of the base (also called the radix).

In programming, if you have an integer value (in the internal format of the computer, probably binary, but it isn't relevant), you can extract the right most digit with the modulus operation.

x = 104
x % 10 #yields 4, the "ones" place

And then you can get "all but" the rightmost digit with integer division (integer division discards the remainder which we no longer need).

x = x / 10 #yields 10
x % 10 #now yields 0, the "tens" place
x = x / 10 #yields 1
x % 10 #now yields 1, the "hundreds" place

So if you do modulus and integer division in a loop (stopping when x == 0), you can output a number in any base.

luser droog
  • 18,988
  • 3
  • 53
  • 105
0

This is basic arithmetic. See binary numeral system & radix wikipedia entries.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547