Convert a hex string to an int
Need some help...
I got this java method:
publicint convertHexStringToInt(String str)
Using shifting and masking I have to process the characters in the String, in order, and generate (and return) the appropriate int value.
I got this bit of code for converting a String of decimal digit characters into an binary integer so its something like this i need:
String s ="123";
int value = 0;
for (int i = 0; i < s.length(); i++){
char c = s.charAt(i);
int d = c & 0x000f;
value *= 10;
value += d;
}
Thanks for you time in helping me learn!!
Message was edited by:
corleone84
Was there a particular question you wanted to ask? The solution would look very much like what you have already, so if you understand that then you should be able to implement it in base 16 instead of 10.
I dont exactly understand HOW this is working. Im not sure of what it is that is causing the conversion to take place.
Is there any particular reason for doing this? The simplest solution would be to use the parseInt method that takes a second parameter, the radix.
If you don't understand a piece of code, print out some values while it's working, so you can see what's going on. For example, try running this:
String s = "123";
int value = 0;
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
System.out.println("char at " + i + ": " + c);
int d = c & 0x000f;
System.out.println("Value of char: " + (int)c);
System.out.println("Lowest nibble of char: " + d);
value *= 10;
System.out.println("value * 10: " + value);
value += d;
System.out.println("value + d: " + value);
System.out.println();
}
> Is there any particular reason for doing this? The
> simplest solution would be to use the parseInt method
> that takes a second parameter, the radix.
I assume it's an assignment of some sort. You know how they are about making students reinvent the wheel.
Thanks for all the help. I'm still in a stump however trying to implement this code:
String s = "123";
int value = 0;
for (int i = 0; i < s.length(); i++){
char c = s.charAt(i);
int d = c & 0x000f;
value *= 10;
value += d;
}
to convert a hex string to a decimal signed value.
Thanks for everyones time!
So you haven't worked out how it works yet?
A char is represented internally by a number, which in the case of the String "123" are:
'1' = 49
'2' = 50
'3' = 51
That doesn't look very helpful at first, but if you look at the numbers in binary form:
'1' = 110001
'2' = 110010
'3' = 110011
You may notice that the rightmost nibble (4 bits) are the numbers 1, 2 and 3, so if you mask out all the other bits you end up with the number that the character represents, i.e.
'1' & 0x0F = 0001 = 1
'2' & 0x0F = 0010 = 2
'3' & 0x0F = 0011 = 3
The loop adds these up and shifts them into the right place by multiplying by 10.
Find out what numbers are used to represent the characters A-F (and a-f perhaps) and maybe you will see a similar pattern that you can use.
WOW...and I had an ASCII chart right in front of me too.
I think what i want to do is if I have an A-F value, I need to add 9 to the rightmost nibble value.
I was thinking maybe an if statement saying if its an A-F value to add 9. I trying to look at how I could shift it to manipulate the value that way too. You got me on the right track though, thanks!
Any further ideas are def welcome.
This is what i got:
int c;
int X;
int value;
String s = "7d";
value = 0;
for (int i = 0; i < s.length(); i++){
c = s.charAt(i);
X = c & 0x40; //only look at the upper 4 bit to see if the char is a Letter
c &= 0x000F; // strip off upper nibble
if (X > 0) //if true then char is [A-F or a-f]
X = c + 9; //Add offset
else
X = c;
value *= 16;
value += (X);
}
System.out.println(value);
}
It works but it doesn't implement shifting.
How can I use shifting to add 9?