DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: converts hexidecimal values to decimal

1. Registered User
Join Date
Dec 2005
Posts
8

## converts hexidecimal values to decimal

Can someone show me in java code how an application can read a string as its a hexdecimal number and converts hexidecimal values to decimal

than you

2. Registered User
Join Date
Dec 2005
Posts
7
Code:
```		String hex = "E0DA453";
String temp = "";
char ch = ' ';
int dec = 0;
int val = 0;
int cnt = 0;
for(int i = hex.length()-1; i >=0 ; i--)
{
ch = hex.charAt(i);
if(ch == 'A')val = 10;
else if(ch == 'B') val = 11;
else if(ch == 'C') val = 12;
else if(ch == 'D') val = 13;
else if(ch == 'E') val = 14;
else if(ch == 'F') val = 15;
else
{
temp+=ch;
val = Integer.parseInt(temp);
}
dec += (val * Math.pow(16, cnt));
temp = "";
cnt++;
}```
something like this works...doubtful that it is the most efficient way, just something i threw together real quick...

note: the string variable hex at the top is what is being changed...u could make that so it is read in so you could use it as a converter and just convert numbers that a user inputs...

hope that helps at least a little...

3. Registered User
Join Date
Aug 2005
Location
Melbourne...Australia
Posts
279
In tips section of DevX..

http://www.devx.com/tips/Tip/20752

4. Registered User
Join Date
Dec 2005
Posts
8
thxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx al0o0o0o0o0ot that was so helpful

5. Registered User
Join Date
Aug 2005
Location
Melbourne...Australia
Posts
279
No problem.. Anytime

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise
 Questions? Contact us. C++ Web Development Wireless Latest Tips Open Source

×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.