static public String CoverterUtf8ToFakeUtf16(String utf8String) throws UnsupportedEncodingException {
byte[] inputBytes = utf8String.getBytes("UTF-8");
printBytes(inputBytes,"input bytes:");
ArrayList list = new ArrayList();
//convert input bytes to utf-16
for(int i=0;i<inputBytes.length;i++){
list.add(new Byte("00"));
list.add(inputBytes[i]);
}
byte[] returnByte = new byte[list.size()];
for(int i=0;i<returnByte.length;i++){
returnByte[i] = (Byte)list.get(i);
}
returnByte = new String(returnByte, Charset.forName("UTF-16")).getBytes("UTF-8");
printBytes(returnByte,"return bytes:");
String fakeUtf16 = new String(returnByte,Charset.forName("UTF-8"));
printBytes(fakeUtf16.getBytes("UTF-8"),"fakeUtf16 bytes:");
return fakeUtf16;
}
Testing
public void fakeUtf16(InputStream requestBodyStream ) throws UnsupportedEncodingException {
String requestBODY = RequestBODY.get(requestBodyStream,Charset.forName("utf-8"));
System.out.println("requestBODY:" + requestBODY);
String fakeUtf16 = UnicodeFormatter.CoverterUtf8ToFakeUtf16(requestBODY);
System.out.println("fakeUtf16:" + fakeUtf16);
}
Console 輸出訊息--fakeUtf16--
charset:UTF-8
requestBODY:中
input bytes:[0] = 0xe4
input bytes:[1] = 0xb8
input bytes:[2] = 0xad
return bytes:[0] = 0xc3
return bytes:[1] = 0xa4
return bytes:[2] = 0xc2
return bytes:[3] = 0xb8
return bytes:[4] = 0xc2
return bytes:[5] = 0xad
fakeUtf16 bytes:[0] = 0xc3
fakeUtf16 bytes:[1] = 0xa4
fakeUtf16 bytes:[2] = 0xc2
fakeUtf16 bytes:[3] = 0xb8
fakeUtf16 bytes:[4] = 0xc2
fakeUtf16 bytes:[5] = 0xad
fakeUtf16:ä¸
fakeUtf16:[0] = 0xfe
fakeUtf16:[1] = 0xff
fakeUtf16:[2] = 0x00
fakeUtf16:[3] = 0xe4
fakeUtf16:[4] = 0x00
fakeUtf16:[5] = 0xb8
fakeUtf16:[6] = 0x00
fakeUtf16:[7] = 0xad
--/fakeUtf16--
沒有留言:
張貼留言
留個話吧:)