Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Refactoring for Software Design Smells: Managing Technical Debt this week in the OO, Patterns, UML and Refactoring forum!

# Hex and Octal

Parimala Somasundaram
Ranch Hand

Joined: Mar 31, 2001
Posts: 41
Can someone please explain how to convert hex and octal to binary and the reverse?
Also how to convert hex and octal to decimal and the reverse?
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Parimala,
Hex/Octal to Binary:
binary digits from for rightmost digits:
2**3, 2**2, 2**1, 2**0
8, 4, 2, 1
Use the above numbers to add up values until it equals the given number.
Hex: uses four binary digits to represent one value.
0 - 9, A (10), B (11), C (12), D (13), E (14), and F (15)
Try it: 0x4A
Octal: uses three binary digits to represent one value.
0 - 7
Try it: 073
----------------------------------------------------------------
Binary to Decimal:
Using what we have already learned:
Try it: 111 011
1*(2**0) + 1*(2**1) + 0*(2**2) + 1*(2**3) + 1*(2**4) + 1*(2**5)
1 + 2 + 0 + 8 + 16 + 32 = 59
Try it: 0100 1010
0*(2**0) + 1*(2**1) + 0*(2**2) + 1*(2**3) + 0*(2**4) + 0*(2**5) + 1*(2**6) + 0*(2**7)
0 + 2 + 0 + 8 + 0 + 0 + 64 + 0 = 74
----------------------------------------------------------------
Octal to Decimal:
Using the same position stuff as binary above we use 8 instead of 2.
Try it: 073
3*(8**0) + 7*(8**1)
3 + 56 = 59
----------------------------------------------------------------
Hex to decimal
Using the same as above except use 16 instead of 8.
Try it: 0x4A
A*(16**0) + 4*(16**1)
10*1 + 4*16 = 74

Enjoy,
Manfred.
Parimala Somasundaram
Ranch Hand

Joined: Mar 31, 2001
Posts: 41
Thanks Manfred.
Is the reverse(converting Binary to Hex or Octal and decimal to Hex or Octal) true as you have explained?
That is to convert decimal to hex first convert to binary and then to hex by grouping in fours? If the binary# is say
100011100
how to we group to convert to hex or octal?
Jo Oehrlein
Greenhorn

Joined: Jan 03, 2001
Posts: 11
To convert binary to Hex, group the binary digits from the right by 4s.
0110011 = 011 0011 = hex 33
To convert binary to Octal, group the binary digits from the right by 3s.
0110011 = 0 110 011 = octal 63
Parimala Somasundaram
Ranch Hand

Joined: Mar 31, 2001
Posts: 41
Thanks.

I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com

subject: Hex and Octal