Skip to content

Instantly share code, notes, and snippets.

@zeroleaf
Created September 30, 2013 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeroleaf/6763499 to your computer and use it in GitHub Desktop.
Save zeroleaf/6763499 to your computer and use it in GitHub Desktop.
import com.zeroleaf.util.Print;
/**
* User: zeroleaf
* Date: 13-9-30
* Time: 12:13
*
* A class that demonstrate get the unicode code of a character
* and encode a unicode code to a character.
*/
public class CharacterCode {
public static void main(String[] args) {
String message = "这是一则消息";
int[] unicodeCodes = unicodeCode(message);
String[] hexUnicodeCodes = hexUnicodeCode(message);
System.out.println("Decimal unicode code is:");
Print.println(unicodeCodes);
// for (int i = 0; i < unicodeCodes.length; i++)
// System.out.print(unicodeCodes[i] + ", ");
System.out.println("\nHex unicode code is:");
Print.println(hexUnicodeCodes);
/* ---------------------------------- */
System.out.println("\n");
String original1 = encodeUnicodeCode(unicodeCodes);
String original2 = encodeHexUnicodeCode(hexUnicodeCodes);
System.out.println(String.format("original1 = %s, original2 = %s\t\t" +
"original1 equals original2 is %b", original1, original2,
original1.equals(original2)));
/*
*Output:
* Decimal unicode code is:
* 36825, 26159, 19968, 21017, 28040, 24687,
* Hex unicode code is:
* 8FD9, 662F, 4E00, 5219, 6D88, 606F,
*
* original1 = 这是一则消息, original2 = 这是一则消息 original1 equals original2 is true
*
*/
}
/**
* Get the unicode code of this character
* @param c the character which you want to get the it's unicode
* @return unicode code of this character
*/
public static int unicodeCode(char c) {
return c;
}
/**
* Get the unicode code in hex string format of this character
* @param c the character which you want to get the it's unicode
* @return the unicode code in hex string format
*/
public static String hexUnicodeCode(char c) {
return Integer.toHexString(unicodeCode(c)).toUpperCase();
}
/**
* Get the unicode code of each character in the string.
* @param string the string which you want to get each character's unicode code.
* @return a array that contains all the character's unicode code in the string.
*/
public static int[] unicodeCode(String string) {
int[] unicodeCodes = new int[string.length()];
for (int i = 0; i < string.length(); i++) {
unicodeCodes[i] = unicodeCode(string.charAt(i));
}
return unicodeCodes;
}
/**
* Get unicode code in hex string value of each character in the string.
* @param string
* @return unicode code in hex string value of each character in the string
* and the order is the same as which in the string.
*/
public static String[] hexUnicodeCode(String string) {
String[] hexUnicodeCodes = new String[string.length()];
for (int i = 0; i < string.length(); i++) {
hexUnicodeCodes[i] = hexUnicodeCode(string.charAt(i));
}
return hexUnicodeCodes;
}
/**
* Encode a (Decimal) unicode code to a character.
* @param code the unicode code of a character
* @return the character which unicode (Decimal) code is the given code
*/
public static char encodeUnicodeCode(int code) {
return (char) code;
}
/**
* Encode a hex unicode code to a character
* @param hexCode hex unicode code of a character
* @return the character which hex unicode code is the given value
*/
public static char encodeHexUnicodeCode(String hexCode) {
return (char) Integer.parseInt(hexCode, 16);
}
/**
* Encode a array of (Decimal) unicode code to a String object
* @param codes a array that contains (Decimal) unicode codes
* @return a String Object which character is encode from the array
*/
public static String encodeUnicodeCode(int[] codes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < codes.length; i++) {
sb.append(encodeUnicodeCode(codes[i]));
}
return sb.toString();
}
/**
* Encode a array of hex unicode code to a String object
* @param vals a array that contains hex unicode codes
* @return a String object which character is encode from the array
*/
public static String encodeHexUnicodeCode(String[] vals) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < vals.length; i++) {
sb.append(encodeHexUnicodeCode(vals[i]));
}
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment