-1

I got Hex (Example: 0x61 0x62 0x63) from Bluetooth socket on read.

I want to get its corresponding ASCII (Example: a b c).

How to do that conversion?

I tried:

String s = "0x56 0x49 0x4e 0x31 0x32 0x33 0x46 0x4f 0x52 
            0x44 0x54 0x52 0x55 0x43 0x4b 0x00 0x38";
StringBuilder sb = new StringBuilder(s.length() / 2);
for (int i = 0; i < s.length(); i+=2) {
    String hex = "" + s.charAt(i) + s.charAt(i+1);
    int ival = Integer.parseInt(hex, 16);
    sb.append((char) ival);
}
String string = sb.toString();
sjain
  • 23,126
  • 28
  • 107
  • 185
  • See this SO post : http://stackoverflow.com/questions/4785654/convert-a-string-of-hex-into-ascii-in-java – vjdhama May 06 '14 at 09:07
  • No, this doesn't removes 0x from the Hex and so doesn't gives the correct results. – sjain May 06 '14 at 09:28
  • Whoever downvoted should read the answers given with comments and also can explain the reason for downvote so that I can understand what's wrong. – sjain May 06 '14 at 09:31

2 Answers2

2

My solution (tested):

final String str_in =
    "0x56 0x49 0x4e 0x31 0x32 0x33 0x46 0x4f 0x52 " +
    "0x44 0x54 0x52 0x55 0x43 0x4b 0x00 0x38";
final String[] arr = str_in.split(" ");
String str_out = "";

// To decimal.
//for (final String s : arr)
//{
//  final String chr = " " + Integer.parseInt(s.replace("0x", ""), 16);
//  str_out += chr;
//}
//System.out.println(str_out); // str_out = "86 73 78 49 50 51 70 79 82 68 84 82 85 67 75 0 56"

// To ASCII
for (final String s : arr)
{
    final char chr = (char) Integer.parseInt(s.replace("0x", ""), 16);
    str_out += " " + chr;
}
System.out.println(str_out); // str_out = "V I N 1 2 3 F O R D T R U C K �� 8" // �� is because of 0x00

[EDIT]

To get rid of the �� , just replace 0x00 with 0x30.
This is the ASCII representation for "0"

Something like:

final String str_in =
    "0x56 0x49 0x4e 0x31 0x32 0x33 0x46 0x4f 0x52 " +
    "0x44 0x54 0x52 0x55 0x43 0x4b 0x00 0x38".replace("0x00", "0x30");
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • I am asking conversion from `Hex to Ascii`. But your proposed solution gives conversion from `Hex to Decimal`. Please correct it. – sjain May 06 '14 at 09:23
  • Sorry, going to change that soon. – Phantômaxx May 06 '14 at 09:24
  • I am getting `V I N 1 2 3 F O R D T R U C K � � 8` in the result. However it should be `VIN123FORDTRUCK08`. I tried using `if(string.contains("??")) string.replace("??", "0");` but nothing happened. Any idea ? – sjain May 06 '14 at 10:00
  • Yes. Just replace `0x00` with `0x30` in the original string. This is the ASCII code for "0". – Phantômaxx May 06 '14 at 10:03
  • +1 for this. I used another solution that gives no space between chars. This is really helpfull. – sjain May 06 '14 at 10:14
  • If you don't want the spaces between chars, just use `str_out += "" + chr;` instead of `str_out += " " + chr;`. Anyway, I must admit that @FD_'s solution is more elegant. – Phantômaxx May 06 '14 at 10:17
1

Something like this should work:

String s = "0x56 0x49 0x4e 0x31 0x32 0x33 0x46 0x4f 0x52 
        0x44 0x54 0x52 0x55 0x43 0x4b 0x00 0x38";
StringBuilder sb = new StringBuilder();
String[] components = s.split(" ");
for (String component : components) {
    int ival = Integer.parseInt(component.replace("0x", ""), 16);
    sb.append((char) ival);
}
String string = sb.toString();
FD_
  • 12,947
  • 4
  • 35
  • 62
  • I am asking conversion from `Hex to Ascii`. But your proposed solution gives conversion from `Hex to Decimal`. Please correct it. – sjain May 06 '14 at 09:26
  • 1
    No, this won't convert to decimal. Anyway, there was a mistake I corrected now, please try again. – FD_ May 06 '14 at 09:28
  • Your code is working great but I am getting `VIN123FORDTRUCK��8` in the result. However it should be `VIN123FORDTRUCK08`. I tried using `if(string.contains("??")) string.replace("??", "0");` but nothing happened. Any idea ? – sjain May 06 '14 at 09:52