Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Best way to convert Hex String to int?

Posted on 1999-07-12
Medium Priority
Last Modified: 2008-10-14
Best way to convert Hex String to int?
eg. "FF" -> 255
P.S. Must work for Java 1.0
Question by:anguslai
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

jasbro earned 200 total points
ID: 1245797
int digitVal(char c){
  if (c=='0') return 0;
  if (c=='1') return 1;
  if (c=='2') return 2;
  if (c=='A') return 10;
  if (c=='F') return 15;
  else return 0;  //may want to return a negative, or throw exception

int parseHex(String hexString){
   int value=0;
   for(i = 0; i<hexString.length; i++){
     value=(value*8) + digitVal(hexString.charAt(i);
   return value;

There also may be a class that does this, I will look around

Expert Comment

ID: 1245798
Or you can use the decode(String nm) method in Integer.  You would have to prepend a '0x' to the beginning of the string (after the optional '-').  So:

if (hexString.charAt(0) == '-') value = Integer.decode("-0x" + hexString.substring(1));
else value = Integer.decode(hexString);

you will have to put this in a try catch block to catch number format exceptions....

Expert Comment

ID: 20184703
But it's like this:

Expert Comment

ID: 22718478
here is the best way to do it

String hex = ffabcc;

int value = Integer.parsInt(hex,16);

Thanks and Regards
Y. Kamesh Rao

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question