字符大小为8位还是16位? [英] Char size 8 bit or 16 bit?

查看:194
本文介绍了字符大小为8位还是16位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://docs.oracle.com/javase/tutorial/ java / nutsandbolts / datatypes.html ,字符大小为16位,即2字节。
不知怎的,我回想起它的8位,即1字节。为了清除我的疑问,我创建了一个单字符a
的文本文件并将其保存。然后我检查了文件的大小,它的1字节,即8位。我很困惑,这是
角色的大小?如果它的2字节,为什么文件大小是1字节,如果它是1字节,为什么链接说2个字节?

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html, char size is 16 bit i.e 2 byte. somehow i recalled its 8 bit i.e 1 byte. To clear my doubt, i created a text file with single character "a" and saved it. Then i inspected the size of file , its 1 byte i.e 8 bit. I am confused whats the size of character ? If its 2 byte , why file size is 1 byte and if it is 1 byte why link says 2 bytes?

推荐答案

A < Java中的code> char 是 UTF-16 代码单元。它不一定是完整的Unicode字符,但它实际上是无符号的16位整数。

A char in Java is a UTF-16 code unit. It's not necessarily a complete Unicode character, but it's effectively an unsigned 16-bit integer.

当您将文本写入文件时(或以其他方式将其转换为字节序列),那么数据将取决于您使用的编码。例如,如果您使用ASCII或ISO-8859-1,那么您可以编写哪些字符非常有限,但每个字符只能是一个字节。如果你使用UTF-16,那么每个Java char 将被转换为两个字节 - 但是一些Unicode字符可能需要4个字节(由两个Java <$ c $表示) c> char values)。

When you write text to a file (or in some other way convert it into a sequence of bytes), then the data will depend on which encoding you use. For example, if you use ASCII or ISO-8859-1 then you're very limited as to which characters you can write, but each character will only be a byte. If you use UTF-16, then each Java char will be converted into exactly two bytes - but some Unicode characters may take four bytes (those represented by two Java char values).

如果您使用 UTF-8 ,然后编码形式的单个Java char 的长度将取决于值。

If you use UTF-8, then the length of even a single Java char in the encoded form will depend on the value.

这篇关于字符大小为8位还是16位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆