Skip to content

Instantly share code, notes, and snippets.

@0guzhan
Last active December 11, 2015 23:29
Show Gist options
  • Save 0guzhan/4677493 to your computer and use it in GitHub Desktop.
Save 0guzhan/4677493 to your computer and use it in GitHub Desktop.
This class provides utility methods for converting char to its decimal-octal-hexadecimal equivalent integer values in String representations. CONSOLE> Decimal: 97 Octal: 0141 Hex: \u0061
package com.blog.oguzhan.charutil;
/**
* This class provides utility methods for converting char
* to its decimal-octal-hexadecimal equivalent integer values in String representations.
*
* @author oguzhan.acargil
* */
public class CharUtil {
public static String toHexASCII(char c) {
return "\\u" + String.format("%04x", (int)c);
}
public static String toOctalASCII(char c) {
return String.format("%04o", (int)c);
}
public static String toDecimalASCII(char c) {
return String.valueOf((int)c);
}
public static void main(String[] args) {
System.out.println("Decimal: " + toDecimalASCII('a'));
System.out.println("Octal: " + toOctalASCII('a'));
System.out.println("Hex: " + toHexASCII('a'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment