String s ="一";
int v=s.charAt(0);
byte[] b=s.getBytes();
System.out.println(b[0]);
System.out.println(b[1]);
System.out.println(v);打印结果:
b[0]=-=46
b[1]=-69
v=19968
也就是说这个一字占用两个字节,数值分别是D2BB,但使用了charAt函数就变成了4E00。这是为什么呢?
java为什么要这么处理,我看了charAt这个函数:
public char charAt(int index) {
if ((index < 0) || (index >= count)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index + offset];
}它加的这个偏移量是多少呢?
这是一种什么编码方式,请教大家
int v=s.charAt(0);
byte[] b=s.getBytes();
System.out.println(b[0]);
System.out.println(b[1]);
System.out.println(v);打印结果:
b[0]=-=46
b[1]=-69
v=19968
也就是说这个一字占用两个字节,数值分别是D2BB,但使用了charAt函数就变成了4E00。这是为什么呢?
java为什么要这么处理,我看了charAt这个函数:
public char charAt(int index) {
if ((index < 0) || (index >= count)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index + offset];
}它加的这个偏移量是多少呢?
这是一种什么编码方式,请教大家
打出来的是汉字的UNICODE,而: byte[] b=s.getBytes();
在WINOWS平台是转换成了GBK了,打出来的b[0]与b[1]是GBK的编码
那在charAt返回值中加入的offset是多少?
我通过tcp/ip的方式和对方通讯,在传输汉字的时候我是不是先要向对方说明采用的是GBK编码还是Unicode编码呢?
2)若是通过tcp/ip的方式和对方通讯,在传输汉字的时候要指明某种编码。如:预先说明好,传输的就是GBK编码的字节串就行了[UTF-8也行,反正要与对方事先讲好]。对方收到一串字节数据后,就按GBK进行解释。
这样做的好处是:对方程序可以不是JAVA,对方可以是C、C++等程序均可。