Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes please help ????   how to convert hexadecimal ,octol to decimal values and back Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "please help ????   how to convert hexadecimal ,octol to decimal values and back" Watch "please help ????   how to convert hexadecimal ,octol to decimal values and back" New topic
Author

please help ???? how to convert hexadecimal ,octol to decimal values and back

felix thomas
Ranch Hand

Joined: Jun 10, 2001
Posts: 89
hi,

please any anyone help me to convert hexadecimal and octal values to decimal
i know the vice a versa i.e. from decimal to hexadecimal ,octal
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

For Decimal to Hex, just divide successively by 16, keeping the whole number result but noting the remainder of each division as you go, until you get a 0 result.
Take decimal 11948
11948 / 16 = 746 rem. 12
746 / 16 = 46 rem. 10
46 / 16 = 2 rem 14
2 / 16 = 0 rem 2
Then, go backwards through your list of remainders and convert each to hex. For this example, you will get 2EAC, which is the Hex representation of Decimal 11948
Same process when you convert Decimal to Octal, only your divisor will be 8.
For Hex to Decimal:
2EAC = (2 * 16^3) + (14 * 16^2) + (10 * 16^1) + (12 * 16^0) = 11948
When converting from Octal, use powers of 8.

[This message has been edited by JUNILU LACAR (edited June 21, 2001).]


Junilu - [How to Ask Questions] [How to Answer Questions]
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Felix,
Its easy, as long as you can perform a power function and some multiplication.
Hex to Dec:
Values 0 to 9 translate directly, A - F map to 10 to 15
Generic formula:
Decimal subValue = Hex digit * (16 ^ digit position)
where digit position starts at zero in the rightmost position and increments by one as you go left.
Once you get all the subtotals then you just add them together.
Examples:
0x4321
1*16^0 + 2*16^1 + 3*16^2 + 4*16^4
1*1 + 2*16 + 3*256 + 4*4096
1 + 32 + 768 + 16384
17185
0x7D
13*16^0 + 7*16^1
13*1 + 7*16
125
Oct to Dec:
Use the same formula but you use 8 instead of 16.
Values 0 - 7 translate directly

Decimal subValue = Oct digit * (8 ^ digit position)
Examples:
076
6 * 8^0 + 7 * 8^1
6*1 + 7*8
6 + 56
62
0432
2 * 8^0 + 3 * 8^1 + 4 * 8^2
2*1 + 3*8 + 4*64
2 + 24 + 256
282
Regards,
Manfred.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Felix,
I have some notes on binary,octal,decimal, hexidecimal conversions posted at http://www.janeg.ca/scjp/oper/binhex.html
Hope they help.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
felix thomas
Ranch Hand

Joined: Jun 10, 2001
Posts: 89
hi,
thanks a lot guys
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please help ???? how to convert hexadecimal ,octol to decimal values and back
 
Similar Threads
Unicode, octal, hexa..... Help
Conversion
how to convert decimals to octal and hexadecimal?
Conversion
decimal ->octal & decimal ->hexadecimal