Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Core Java Code Examples

Converting String to Hex

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