Example ASCII to Hex Translation
ASCII Character string: cake
ASCII Decimal code: 99 97 107 101
ASCII Hexadecimal code (what goes on the wire): 63616B65
Below are a couple simple Java functions to perform these string translations:
Code
public class Decoder
{
public String hexToString(String hex)
{
String str = "";
if (hex != null)
for (int i=0; i < hex.length()-1; i+=2)
try
{
str += (char) Integer.parseInt(hex.substring(i, i+2),16);
}
catch (Exception e)
{
return "";
}
return str;
}
public String StringtoHex(String str)
{
String hex = "";
if (str != null)
for (int i=0; i < str.length(); i++)
hex += Integer.toHexString((int)str.charAt(i));
return hex;
}
public static void main(String[] args)
{
Decoder decoder = new Decoder();
System.out.println(decoder.StringtoHex("test"));
System.out.println(decoder.hexToString("74657374"));
}
Output74657374
test
Copyright ©1993-2024 Joey E Whelan, All rights reserved.