In my program i am getting a string in hexa format. i want to convert that to string. How to do it ?
Thanks and Regards. Pa开发者_Python百科rvathi
Use the following code to convert hexadecimal into string
String hexadecimalnumber = "00000011";
BigInteger big = new BigInteger(hexadecimalnumber);
String requiredString = big.toString(16);
System.out.println("...data..."+requiredString);
Thanks Deepak
String hex = "ff";
hex = Integer.toString(Integer.parseInt(hex, 16));
class Test
{
private static int hextoint(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
}
if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
}
if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
}
return -1;
}
private static String hexdec(String str) {
int len = str.length();
if(len % 2 != 0){
return null;
}
byte[] buf = new byte[len/2];
int size = 0;
for (int i = 0; i < len; i += 2) {
char c1 = str.charAt(i);
char c2 = str.charAt(i + 1);
int b = (hextoint(c1) << 4) + hextoint(c2);
buf[size++] = (byte)b;
}
return new String(buf, 0, size);
}
public static void main(String[] args)
{
String str = "616263";
String out = hexdec(str);
System.out.println(out);
}
}
精彩评论