当前位置:编程学习 > JAVA >>

char字节问题

String s1="m";
String s2="我";
System.out.println(s1.getBytes().length);
System.out.println(s2.getBytes().length);
输出1 2
但是java中char类型的不是都是两个字节么。。。为什么s1.getBytes().length只打印出 1

--------------------编程问答-------------------- 我认为char是一个字节。

引用楼主  的回复:
String s1="m";
String s2="我";
System.out.println(s1.getBytes().length);
System.out.println(s2.getBytes().length);
输出1 2
但是java中char类型的不是都是两个字节么。。。为什么s1.getBytes().length只打印出 1
--------------------编程问答-------------------- 一个汉字是两个字节。你要是都按字符处理,就是一样的 --------------------编程问答-------------------- String de  getBytes() 方法是根据其编码方式进行转换的,默认的是"GBK",这样的话,ascii字符占一个字节,而汉字占2个。
unicode 都是2字节。

utf-8,ascii占一个,汉字多数是3个(有时不是3个)。

楼主运行一下下面的代码看看:
public class CharBytes
{
public static void main(String[] args)
{
try
{
String s1="m";
String s2="我";
System.out.println("defualt code:\t the length in bytes of s1 is "
+s1.getBytes().length);
System.out.println("defualt code:\t the length in bytes of s2 is "
+s2.getBytes().length);
System.out.println("GBK:\t\t the length in bytes of s1 is "
+s1.getBytes("GBK").length);
System.out.println("GBK:\t\t the length in bytes of s2 is "
+s2.getBytes("GBK").length);

System.out.println("unicode:\t the length in bytes of s1 is "
+(s1.getBytes("Unicode").length-2));//生成unicode 
System.out.println("unicode:\t the length in bytes of s2 is "//会有2个字节标志。
+(s2.getBytes("Unicode").length-2));
System.out.println("utf-8:\t\t the length in bytes of s1 is "
+s1.getBytes("Utf-8").length);
System.out.println("utf-8:\t\t the length in bytes of s2 is "
+s2.getBytes("utf-8").length);

}
catch(Exception uee)
{
uee.getStackTrace();
}
}
}

结果:
defualt code:    the length in bytes of s1 is 1
defualt code:    the length in bytes of s2 is 2

GBK:             the length in bytes of s1 is 1
GBK:             the length in bytes of s2 is 2

unicode:         the length in bytes of s1 is 2
unicode:         the length in bytes of s2 is 2

utf-8:           the length in bytes of s1 is 1
utf-8:           the length in bytes of s2 is 3
--------------------编程问答-------------------- 楼主你好。

Java中字符占2个字节。

getByte()使用平台默认的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中;因为String s1="m";s1这个字符串只有一个字符,故解码为字节序列的时候只有8位,即一个字节,所以长度为1。而中文字符占用2个字节,故长度为2. --------------------编程问答-------------------- 中文字节一般是2个,也有可能是三个,这个要根据编码方式的不同决定的 --------------------编程问答-------------------- java中默认char用Unicode编码,所以是两个字节的,在java中英文字符和标点符号等的unicode编码实际是高八位补0扩展了ASCII码,所以在内存0,楼主可以用字节流把这两个变量写到文件里,然后用16进制编辑工具查看一下就知道了 --------------------编程问答-------------------- 你会发现, 比如你写入一个'a' 在文件里存的是 00 61 --------------------编程问答--------------------
引用 3 楼  的回复:
String de  getBytes() 方法是根据其编码方式进行转换的,默认的是"GBK",这样的话,ascii字符占一个字节,而汉字占2个。
unicode 都是2字节。

utf-8,ascii占一个,汉字多数是3个(有时不是3个)。

楼主运行一下下面的代码看看:
Java code
public class CharBytes
{
    public static v……
正解 --------------------编程问答--------------------
引用 2 楼  的回复:
一个汉字是两个字节。你要是都按字符处理,就是一样的

理解了 。。 谢了~ --------------------编程问答-------------------- 问题出在getBytes --------------------编程问答--------------------
引用 3 楼  的回复:
String de  getBytes() 方法是根据其编码方式进行转换的,默认的是"GBK",这样的话,ascii字符占一个字节,而汉字占2个。
unicode 都是2字节。

utf-8,ascii占一个,汉字多数是3个(有时不是3个)。

楼主运行一下下面的代码看看:
Java code
public class CharBytes
{
    public static v……



 支持。楼主结贴啊。什么素质。 --------------------编程问答-------------------- 4楼说的有道理。 --------------------编程问答-------------------- string s1="m";此时s1是占有两个字节的空间的,是有字母m和转义字符/n组成的,
string s2="我";占有两个字节的空间,但是由getbytes()转换成字节数组时转移字符是不算在内的,s1得到的m,
s2得到的是"我"的字节组成,他是有两个字节构成的 --------------------编程问答-------------------- char 只能是一个字节,汉字一般是两个.. --------------------编程问答-------------------- getBytes
public byte[] getBytes()使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 
当此字符串不能使用默认的字符集编码时,此方法的行为没有指定。如果需要对编码过程进行更多控制,则应该使用 CharsetEncoder 类。 


String s1="m"; //“m”是一个字符串,如果是‘m’就是一个字符,字符串就占用两个字节,一个char字符占用一个字节,但是字符串有‘\0’作为结束标志,占一个字符。
String s2="我"; 一个汉子占用两个字节; --------------------编程问答--------------------
引用 3 楼  的回复:
String de getBytes() 方法是根据其编码方式进行转换的,默认的是"GBK",这样的话,ascii字符占一个字节,而汉字占2个。
unicode 都是2字节。

utf-8,ascii占一个,汉字多数是3个(有时不是3个)。

楼主运行一下下面的代码看看:

Java code
public class CharBytes
{
    public static……

正解 --------------------编程问答-------------------- 为什么我输出的是3

引用楼主  的回复:
String s1="m";
String s2="我";
System.out.println(s1.getBytes().length);
System.out.println(s2.getBytes().length);
输出1 2
但是java中char类型的不是都是两个字节么。。。为什么s1.getBytes().length只打印出 1
补充:Java ,  Java SE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,