DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
To Encode String To Hex
// To encode string to hex
public static String encodeStringToHex(String sourceText) {
byte[] rawData = sourceText.getBytes();
StringBuffer hexText = new StringBuffer();
String initialHex = null;
int initHexLength = 0;
for (int i = 0; i < rawData.length; i++) {
int positiveValue = rawData[i] & 0x000000FF;
initialHex = Integer.toHexString(positiveValue);
initHexLength = initialHex.length();
while (initHexLength++ < 2) {
hexText.append("0");
}
hexText.append(initialHex);
}
return hexText.toString();
}





