编码和解码字符串时出现问题。如果我将字符串编码为a到b,则b到c表示它可以正常工作。如果我转换finalstring意味着我得到了一些额外的字符。请查看我的输出,以使您的说明在输出的末尾变得多余。
这是我的代码。
public class DoubleByteReverse {
public static void main(String args[]) {
try{
String a = new String("基本形");
System.out.println("a value "+a);
String b=new String(a.getBytes("Cp939"), "Cp500");
System.out.println("b value "+b);
String c=new String(b.getBytes("Cp500"), "Cp939");
System.out.println("c value "+c);
String g = new String("ã1áÃã°");
String x = "0x0E";
byte[] bytes = hexStringToByteArray(x);
String st = new String(bytes,"Cp500");
//System.out.println(st);
String y = "0x0F";
byte[] bytes1 = hexStringToByteArray(y);
String en = new String(bytes1,"Cp500");
//System.out.println(en);
String finalstring =new String(st+g+en);
System.out.println("whole string "+finalstring);
String output=new String(finalstring.getBytes("Cp500"),"Cp939");
System.out.println("output "+output);
}
catch (UnsupportedEncodingException e){}
}
public static byte[] hexStringToByteArray(String hex) {
int l = hex.length();
byte[] data = new byte[l/2];
for (int i = 0; i < l; i += 2) {
data[i/2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
+ Character.digit(hex.charAt(i+1), 16));
}
return data;
}
}
输出:
a value 基本形
b value ã1áÃã°
c value 基本形
whole string ã1áÃã°
output 基本形�
您所做的hexStringToByteArray
工作似乎无法正常工作,但是如果您更改此值的finalstring
值,new String("\u000E" + g + "\u000F")
它将按预期工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句