views:

293

answers:

5

Hey guys,

Just wondering on how I would go about converting binary to hexadecimal?? Would I first have to convert the binary to decimal and then to hexadecimal??

For example, 101101001.101110101010011

How would I go about converting a complex binary such as the above to hexadecimal?

Thanks in advance

+4  A: 

Each 4 bits of a binary number represents a hexadecimal digit. So the best way to convert from binary to hexadecimal is to pad the binary number with leading zeroes so that the number of bits is divisible by four.

Then you process four bits at a time and convert them to a single hexadecimal digit:

0000 -> 0
0001 -> 1
0010 -> 2
....
1110 -> E
1111 -> F
Andreas Brinck
A: 

The simplest approach, especially if you already can convert from binary digits to internal numeric representation and from internal numeric representation to hexadecimal digits, is to go binary->internal->hex. I say internal and not decimal, because even though it may print as decimal, it is actually being stored internally in binary format. That said, it is possible to go straight from one to the other. This does not apply to your specific example, but in many cases when converting from binary to hex, you can go four digits at a time, and simply lookup the corresponding hex values in a table. There are all sorts of ways to convert.

Michael Aaron Safyan
+1  A: 

You could simply have a small hash table, or other mapping converting each quadruplet of binary digits (as a string, assuming that's your input) into the corresponding hex digit (0 to 9, A to F) for the output string. You'll have to bunch the input bits up by 4, left-padding before the '.' and right-padding after it, with 0 in both cases, as needed.

So...:

  1. locate the '.'

  2. left of the '.', bunch by 4, left-padding the last bunch, going leftwards: in your example, 1001 leftmost, then 0110, finally 0001 (left-padding), that's it;

  3. ditto to the right -- in your example 1011, then 1010, then 1010, finally 0110 (right-padding)

  4. each bunch of 4 binary digits, via a hash or other form of hashing, turns into the hex digit to put in that place in the output string.

Want some pseudo-code for it, e.g., Python?

Alex Martelli
+2  A: 

No, you don't convert to decimal and then to hexadecimal, you convert to a numeric value, and then to hexadecimal.

(Decimal is also a textual representation of a number, just like binary and hexadecimal. Although decimal representation is used by default, a number doesn't have a textual representation in itself.)

As a hexadecimal digit corresponds to four binary digits you don't have to convert the entire string to a number, you can do it four binary digits at a time.

First fill up the binary number so that it has full groups of four digits:

000101101001.1011101010100110

Then you can convert each group to a number, and then to hexadecimal:

0001 0110 1001.1011 1010 1010 0110

169.BAA6

Alternatively, you can split the number into the two parts before and after the period and convert those from binary. The part before the period can be converted stright off, but the part after has to be padded to be correct.

Example in C#:

string binary = "101101001.101110101010011";

string[] parts = binary.Split('.');
while (parts[1].Length % 4 != 0) {
  parts[1] += '0';
}

string result =
  Convert.ToInt32(parts[0], 2).ToString("X") +
  "." +
  Convert.ToInt32(parts[1], 2).ToString("X");
Guffa
A: 

BIN to HEX


Binary and hex are natively compatible. Just group 4 binary digits(bits) and substitute the corresponding HEX-digit.

More reference here:

CVS-2600Hertz