programming-examples/java/Core_Java/Converting String to Hex.java

34 lines
911 B
Java
Raw Normal View History

2019-11-15 12:59:38 +01:00
Converting String to Hex
public static String stringToHex(String base)
{
StringBuffer buffer = new StringBuffer();
int intValue;
for(int x = 0; x < base.length(); x++)
{
int cursor = 0;
intValue = base.charAt(x);
String binaryChar = new String(Integer.toBinaryString(base.charAt(x)));
for(int i = 0; i < binaryChar.length(); i++)
{
if(binaryChar.charAt(i) == '1')
{
cursor += 1;
}
}
if((cursor % 2) > 0)
{
intValue += 128;
}
buffer.append(Integer.toHexString(intValue) + " ");
}
return buffer.toString();
}
public static void main(String[] args)
{
String s = "The cat in the hat";
System.out.println(s);
System.out.println(test1.stringToHex(s));
}