姓名跟住址這類資料存取往往都會跟CharSet有關,如果某個環節沒弄好就是亂碼+破版來回報,做個備忘嚕!!
String CharSet = "UTF-8";
byte[] bytes_rowdata = s_addr.getBytes(CharSet );
String s_Addr = ArrayCopy2Str(bytes_rowdata, 0, 11, 20, CharSet );
/*
* i_posstr: 相對起始位置
* i_offset: 偏移位
* len: 取的字元長度
* CharSet: 字元編碼
*/
private String ArrayCopy2Str(byte[] bytes_rowdata, int i_posstr, int i_offset, int len, String CharSet ) {
String s_val = "";
try {
vResult = new byte[len];
System.arraycopy( bytes_rowdata, i_posstr + i_offset, vResult, 0, len);
//還原字串也要設定字元編碼,如果沒給那一定機率會變亂碼
s_val = new String(vResult, CharSet);
} catch (Exception ex) {
System.out.println("ArrayCopy2Str has exception:" + ex.toString());
}
return s_val;
}
try {
vResult = new byte[len];
System.arraycopy( bytes_rowdata, i_posstr + i_offset, vResult, 0, len);
//還原字串也要設定字元編碼,如果沒給那一定機率會變亂碼
s_val = new String(vResult, CharSet);
} catch (Exception ex) {
System.out.println("ArrayCopy2Str has exception:" + ex.toString());
}
return s_val;
}
沒有留言:
張貼留言