Created
November 29, 2018 06:33
-
-
Save boileryao/fd9905dd2d623949130d237203132046 to your computer and use it in GitHub Desktop.
Java Encoding Test Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package strings; | |
import java.nio.charset.Charset; | |
import java.util.Arrays; | |
/** | |
* Created by boileryao on 11/29/2018. | |
* Class: Strings | |
*/ | |
public class Strings { | |
static int count(String str, char ch) { | |
int cnt = 0; | |
for (char c : str.toCharArray()) { | |
if (c == ch) { | |
cnt++; | |
} | |
} | |
return cnt; | |
} | |
public static void main(String[] args) { | |
String name = "锅炉🐷咦丶恸"; | |
System.out.println(Arrays.toString(name.toCharArray())); | |
System.out.println(name.length()); | |
for (char c : name.toCharArray()) { | |
System.out.println(c + " " + (int) (c)); | |
} | |
name.codePoints().forEach(System.out::println); | |
System.out.println("Default Encoding is " + Charset.defaultCharset().name()); | |
encodingShow("12"); | |
encodingShow("{+#$`"); | |
encodingShow("中国"); | |
encodingShow("锅炉工🐒"); | |
} | |
private static int[] BIT_MASK = new int[]{ | |
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 | |
}; | |
private static void encodingShow(String str) { | |
System.out.println("Checking: " + str); | |
byte[] bytes = str.getBytes(); | |
printByteBitArray(bytes); | |
printStringWithEncoding(bytes, Charset.forName("ASCII")); | |
printStringWithEncoding(bytes, Charset.forName("UTF-8")); | |
printStringWithEncoding(bytes, Charset.forName("UTF-16")); | |
printStringWithEncoding(bytes, Charset.forName("Unicode")); | |
printStringWithEncoding(bytes, Charset.forName("ISO-8859-1")); | |
} | |
private static void printStringWithEncoding(byte[] bytes, Charset charset) { | |
String str = new String(bytes, charset); | |
System.out.printf("%10s : %-20s\n", charset.name(), str); | |
} | |
private static void printByteBitArray(byte[] bytes) { | |
for (byte bt : bytes) { | |
for (int i = BIT_MASK.length - 1; i >= 0; i--) { | |
System.out.print((BIT_MASK[i] & bt) > 0 ? '1' : '0'); | |
} | |
System.out.print(" "); | |
} | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment