Summary

This article tell u how to simulate string write to db that come from some application.You will know how it works

Preliminary knowledge

Encodes this String into a sequence of bytes using the named charset,storing the result into a new byte array.

Constructs a new String by decoding the specified array of bytes using the specified charset.

Demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
try {
// simulate a db charset UTF-8
System.setOut(new PrintStream(new File("a.txt"), "UTF-8"));

// your edit is UTF-8,complie using UTF-8
String s = "測試";
byte[] bs = s.getBytes("UTF-8"); //encoding to {e6,b8,ac,e8,a9,a6}

// for (int i = 0 ;i<bs.length;i++) {
// System.out.println(Integer.toHexString(bs[i]));
// }

// simulate get response parameter using big5
String param = new String(bs, "Big5");// decoding bs array using Big5.You can use visit Query BIG-5 Code website using bs array,and will get 皜祈岫 decoding(e6 b8 ac e8 a9 a6)

// simulate write to db
System.out.println(param);//param be encoded using UTF-8 in the txt file and will see 皜祈岫 encoding utf8(e7 9a 9c e7 a5 88 e5 b2 ab)
} catch (Exception e) {

}

encoding and decoding


reference