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.

1

4 Answers

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.

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)
4

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.

1

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.