-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCharsetDemo.java
More file actions
35 lines (30 loc) · 979 Bytes
/
Copy pathCharsetDemo.java
File metadata and controls
35 lines (30 loc) · 979 Bytes
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
34
35
import java.nio.charset.Charset;
import java.nio.ByteBuffer;
public class CharsetDemo{
public static void main(String[] args){
String msg = "ξξεγβνεμ θθδμχρ";
if(args.length > 0){
msg = args[0];
}
String[] csNames = {"US-ASCII", "ISO-8859-1", "UTF-8", "UTF-16BE", "UTF-16LE", "UTF-16"};
encode(msg, Charset.defaultCharset());
for(String csName : csNames){
encode(msg, Charset.forName(csName));
}
}
static void encode(String msg, Charset charset){
System.out.println("Charset: " + charset.toString());
System.out.println("Message: " + msg);
ByteBuffer buffer = charset.encode(msg);
System.out.println("Encoded: ");
for(int i=0; i< buffer.limit(); i++){
int _byte = Byte.toUnsignedInt(buffer.get(i));
char ch = (char)_byte;
if(Character.isWhitespace(ch) || Character.isISOControl(ch)){
ch = '\u0000'; // empty string
}
System.out.printf("%2d: %02x (%c)%n", i, _byte, ch);
}
System.out.println();
}
}