1

HI I have to tried multiple ways to convert Hex String to ASCII String but not getting success. While before I have done the same but now I am not able to achieve it.

My Code is

private static String hexToASCII(String hexValue)
{
    StringBuilder output = new StringBuilder("");
    for (int i = 0; i < hexValue.length(); i += 2)
    {
        String str = hexValue.substring(i, i + 2);
        output.append((char) Integer.parseInt(str, 16));
    }
    return output.toString();
}

but it is returning garbage value like b��¡

and my Hex String is

  621c8002008a820101a10a8c0341c2009c0341c2008302010288008a0105

Please help me if someone has also suffered from the same issue and fixed it.

Thanks ....

Neha Shukla
  • 3,572
  • 5
  • 38
  • 69

1 Answers1

1

Try this out

public class HextoAsscii {

    public static void main(String args[])
    {
        String hex="621c8002008a820101a10a8c0341c2009c0341c2008302010288008a0105";
        String str="";
        str= hexToASCII(hex);

    }
    private static String hexToASCII(String hexValue)
    {
        StringBuilder output = new StringBuilder("");

        for (int i = 0; i < hexValue.length(); i += 2)
        {
            if(i+2<=hexValue.length())
            {
            String str = hexValue.substring(i, i + 2);
            output.append(Integer.parseInt(str, 16));
            }
        }
        System.out.println(output.toString());
        return output.toString();
    }
}
Dilip Marpu
  • 21
  • 1
  • 3
  • hey it is returning response as "982812820138130111611014036519401563651940131212136013815" while I want String – Neha Shukla Oct 09 '14 at 10:50
  • yes it is the Ascii value in integer , i have removed the char from the output.append((char)Integer.parseInt(str,16))---http://tomeko.net/online_tools/hex_to_ascii.php?lang=en- hex to ascii ... even though the char is present you are getting the correct Ascii but you are getting some special symbols , even those special symbols represent the ascii code - click on the below link to get an idea - http://www.asciitable.com/ - see extended ascii codes. – Dilip Marpu Oct 09 '14 at 10:53