Skip to content

Commit 06e7fac

Browse files
committed
Added HexToAscii example.
1 parent 02dda99 commit 06e7fac

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.hexToAscii;
2+
3+
public class HexToAscii {
4+
5+
public static void main(String[] args) {
6+
7+
String demoString = "http://www.baeldung.com/jackson-serialize-dates";
8+
9+
System.out.println("Original statement: " + demoString);
10+
11+
String hexEquivalent = asciiToHex(demoString);
12+
System.out.println("Hex equivalent: " + hexEquivalent);
13+
14+
String asciiEquivalent = hexToASCII(hexEquivalent);
15+
System.out.println("Ascii equivalent: " + asciiEquivalent);
16+
}
17+
18+
private static String asciiToHex(String asciiStr) {
19+
20+
char[] chars = asciiStr.toCharArray();
21+
StringBuilder hex = new StringBuilder();
22+
for (char ch : chars) {
23+
hex.append(Integer.toHexString((int) ch));
24+
}
25+
26+
return hex.toString();
27+
}
28+
29+
private static String hexToASCII(String hexStr) {
30+
31+
StringBuilder output = new StringBuilder("");
32+
for (int i = 0; i < hexStr.length(); i += 2) {
33+
String str = hexStr.substring(i, i + 2);
34+
output.append((char) Integer.parseInt(str, 16));
35+
}
36+
return output.toString();
37+
}
38+
}

0 commit comments

Comments
 (0)