Hex to Integer
trytryagain
Posts: 26
Peter,
You·where kind enough to supply me with the
routine below to convert·an integer·to hex:
///////////////////////////////
// Integer To·Hex Convertor
///////////////////////////////
public static String Dec2Hex(int d) {
stemp.clear();
int digit = '0' + ((d>>4)&0x0F);
if (digit > '9') digit += 7;
stemp.append((char)digit);
digit = '0' + (d&0x0F);
if (digit > '9') digit += 7;
stemp.append((char)digit);
return stemp.toString();} //End Dec2Hex
I am receiving a hex value in one of my
receive buffers and now have to convert
the other way Hex to Integer.
///////////////////////////////
// Hex to Integer Convertor
///////////////////////////////
public static int GetEmpNo(){
receiveBuffer.clear();
receiveBuffer.append("2A");
int x = Hex2Int(1);
int y = Hex2Int(0) * 16 + x;
System.out.println(y); //converted 2A to 42
return y;
}
public static int Hex2Int(int pos){
int x;
switch(receiveBuffer.charAt(pos)){
case '0': x = 0; break;
case '1': x = 1; break;
case '2': x = 2; break;
case '3': x = 3; break;
case '4': x = 4; break;
case '5': x = 5; break;
case '6': x = 6; break;
case '7': x = 7; break;
case '8': x = 8; break;
case '9': x = 9; break;
case 'A': x = 10;break;
case 'B': x = 11;break;
case 'C': x = 12;break;
case 'D': x = 13;break;
case 'E': x = 14;break;
case 'F': x = 15;break;
default: x = 0;
}
return x;
}
This works but I was wondering if you
had any ideas on a cleaner and hopefully
faster method without the switch. My
method is also limited to a 2 character
hex value which I would like to fix.
Thanks as always,
Jeff (trytryagain)
·
You·where kind enough to supply me with the
routine below to convert·an integer·to hex:
///////////////////////////////
// Integer To·Hex Convertor
///////////////////////////////
public static String Dec2Hex(int d) {
stemp.clear();
int digit = '0' + ((d>>4)&0x0F);
if (digit > '9') digit += 7;
stemp.append((char)digit);
digit = '0' + (d&0x0F);
if (digit > '9') digit += 7;
stemp.append((char)digit);
return stemp.toString();} //End Dec2Hex
I am receiving a hex value in one of my
receive buffers and now have to convert
the other way Hex to Integer.
///////////////////////////////
// Hex to Integer Convertor
///////////////////////////////
public static int GetEmpNo(){
receiveBuffer.clear();
receiveBuffer.append("2A");
int x = Hex2Int(1);
int y = Hex2Int(0) * 16 + x;
System.out.println(y); //converted 2A to 42
return y;
}
public static int Hex2Int(int pos){
int x;
switch(receiveBuffer.charAt(pos)){
case '0': x = 0; break;
case '1': x = 1; break;
case '2': x = 2; break;
case '3': x = 3; break;
case '4': x = 4; break;
case '5': x = 5; break;
case '6': x = 6; break;
case '7': x = 7; break;
case '8': x = 8; break;
case '9': x = 9; break;
case 'A': x = 10;break;
case 'B': x = 11;break;
case 'C': x = 12;break;
case 'D': x = 13;break;
case 'E': x = 14;break;
case 'F': x = 15;break;
default: x = 0;
}
return x;
}
This works but I was wondering if you
had any ideas on a cleaner and hopefully
faster method without the switch. My
method is also limited to a 2 character
hex value which I would like to fix.
Thanks as always,
Jeff (trytryagain)
·
Comments
char buf[noparse]/noparse = new char[noparse][[/noparse]7]; //assume buf contains "2A",0
int result[noparse]/noparse = new int[noparse][[/noparse]1]; //placeholder for decimal value
Format.sscanf(buf,"%x",result);
buf is scanned for hexadecimal digits '0' to '9', 'A' to 'F', 'a' to 'f'
sscanf stops scanning at the first non-hexdigit.
result[noparse][[/noparse]0] contains the decimal value of the hexstring in buf.
regards peter