字符串成字符? (JAVA) [英] Char into byte? (Java)
问题描述
为什么会发生这种情况:
How come this happens:
char a = '\uffff'; //Highest value that char can take - 65535
byte b = (byte)a; //Casting a 16-bit value into 8-bit data type...! Isn't data lost here?
char c = (char)b; //Let's get the value back
int d = (int)c;
System.out.println(d); //65535... how?
基本上,我看到 char
是16位。因此,如果将其转换为字节
,为什么没有数据丢失? (在投入到int之后,值是相同的)
Basically, I saw that a char
is 16-bit. Therefore, if you cast it into a byte
, how come no data is lost? (Value is the same after casting into an int)
提前感谢您回答我这个无知的小问题。 :P
Thanks in advance for answering this little ignorant question of mine. :P
编辑:哇,发现我的原始输出实际上按预期进行了,但我刚刚更新了上面的代码。基本上,将一个字符强制转换为一个字节,然后将其强制转换为char,并保留其原始的2字节值。这是怎么发生的?
Woah, found out that my original output actually did as expected, but I just updated the code above. Basically, a character is cast into a byte and then cast back into a char, and its original, 2-byte value is retained. How does this happen?
推荐答案
正如trojanfoe所说,你对代码结果的混淆部分是由于符号扩展。我会尝试添加一个更详细的解释,这可能有助于您的混淆。
As trojanfoe states, your confusion on the results of your code is partly due to sign-extension. I'll try to add a more detailed explanation that may help with your confusion.
char a = '\uffff';
byte b = (byte)a; // b = 0xFF
如你所知,这会导致信息丢失。这被认为是缩小转换。将char转换为字节只丢弃除n个最低位之外的所有位。
结果为: 0xFFFF - > 0xFF
As you noted, this DOES result in the loss of information. This is considered a narrowing conversion. Converting a char to a byte "simply discards all but the n lowest order bits".
The result is: 0xFFFF -> 0xFF
char c = (char)b; // c = 0xFFFF
将字节转换为char被视为特殊转换。它实际上执行两次转换。首先,字节是SIGN扩展的(新的高阶位从旧符号位复制)到int(正常的加宽转换)。其次,int转换为具有缩小转换的char。
结果是: 0xFF - > 0xFFFFFFFF - > 0xFFFF
Converting a byte to a char is considered a special conversion. It actually performs TWO conversions. First, the byte is SIGN-extended (the new high order bits are copied from the old sign bit) to an int (a normal widening conversion). Second, the int is converted to a char with a narrowing conversion.
The result is: 0xFF -> 0xFFFFFFFF -> 0xFFFF
int d = (int)c; // d = 0x0000FFFF
将char转换为int被认为是扩大转换。当char类型被扩展为整数类型时,它是ZERO扩展的(新的高位比特被设置为0)。
结果是: 0xFFFF - > 0x0000FFFF
。打印时,这将为您提供65535。
Converting a char to an int is considered a widening conversion. When a char type is widened to an integral type, it is ZERO-extended (the new high order bits are set to 0).
The result is: 0xFFFF -> 0x0000FFFF
. When printed, this will give you 65535.
我提供的三个链接是有关原始类型转换的官方Java语言规范详细信息。我强烈建议你看看。它们并不是非常冗长(在这种情况下相对简单)。它详细说明了java将在幕后进行类型转换的内容。对于许多开发人员来说,这是一个常见的误解区域。如果您仍然对任何步骤感到困惑,请发表评论。
The three links I provided are the official Java Language Specification details on primitive type conversions. I HIGHLY recommend you take a look. They are not terribly verbose (and in this case relatively straightforward). It details exactly what java will do behind the scenes with type conversions. This is a common area of misunderstanding for many developers. Post a comment if you are still confused with any step.
这篇关于字符串成字符? (JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!