ASCII 和 Hex 互转

Author Avatar
智灵 6月 23, 2020

Many times we do need to convert the string values ASCII from/to Hex format. In this small, post I am giving you two small code snippets which you can utilize to convert a string from Hex to ASCII or ASCII to Hex, as you want.

Overall conversion logic looks like this:

1
Hex   --  Decimal  --  ASCII

Convert ASCII to Hex

It is done in following steps:

  1. Convert String to char array
  2. Cast it to Integer
  3. Use Integer.toHexString() to convert it to Hex

ASCII to Hex Example Code

1
private` `static` `String asciiToHex(String asciiValue)``{``  ``char``[] chars = asciiValue.toCharArray();``  ``StringBuffer hex = ``new` `StringBuffer();``  ``for` `(``int` `i = ``0``; i < chars.length; i++)``  ``{``    ``hex.append(Integer.toHexString((``int``) chars[i]));``  ``}``  ``return` `hex.toString();``}

Convert Hex to ASCII

It is done in following steps:

  1. Cut the Hex value in 2 chars groups
  2. Convert it to base 16 Integer using Integer.parseInt(hex, 16) and cast to char
  3. Append all chars in StringBuilder

Hex to ASCII Example Code

1
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();``}

Now let’s test above methods with sample input data.

Complete Example for Hex to ASCII and ASCII to Hex Conversion

In this example, I am converting a string “//howtodoinjava.com” to first in hex format, and then convert that hex string to again in ASCII value. This converted ASCII value should be equal to original string i.e. “//howtodoinjava.com”.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class HexAsciiConversionExamples
{
public static void main(String[] args)
{
String demoString = "//howtodoinjava.com";
//Original String
System.out.println("Original String: "+ demoString);

String hexEquivalent = asciiToHex(demoString);
//Hex value of original String
System.out.println("Hex String: "+ hexEquivalent);

String asciiEquivalent = hexToASCII(hexEquivalent);
//ASCII value obtained from Hex value
System.out.println("Ascii String: "+ asciiEquivalent);
}

private static String asciiToHex(String asciiValue)
{
char[] chars = asciiValue.toCharArray();
StringBuffer hex = new StringBuffer();
for (int i = 0; i < chars.length; i++)
{
hex.append(Integer.toHexString((int) chars[i]));
}
return hex.toString();
}

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();
}
}

Output:

Original String: //howtodoinjava.com
Hex String: 687474703a2f2f686f77746f646f696e6a6176612e636f6d
Ascii String: //howtodoinjava.com

Happy Learning !!

Reprinted here