从int到byte的有损转换? [英] lossy conversion from int to byte?
问题描述
我作为练习做的简单加密算法给了我一个从int到byte的可能有损转换?但我不知道为什么它给我这个错误任何想法?
my simple encryption algorithim i am doing as an exercise is giving me a possible lossy conversion from int to byte? but i dont know why it is giving me this error any ideas?
public class Rot13Crypt extends CryptStream
{
public Rot13Crypt(StreamPair theStreams)
{
super(theStreams);
}
protected byte [] cryptData(byte [] data, int len)
{
byte [] cryptedByte = new byte[len];
for(int i = 0; i < len; i++)
{
cryptedByte[i] = (data[i] + 13) % 256;
}
return cryptedByte;
}
protected byte [] decryptData(byte [] data, int len)
{
byte [] decryptedByte = new byte[len];
for(int i = 0; i < len; i++)
{
decryptedByte[i] = (data[i] * 256) - 13;
}
return decryptedByte;
}
}
推荐答案
它提供该错误消息是因为您试图将int值表达式的值赋给 byte
。这是一个潜在的有损操作:直观地说,您不能将所有可能的 int
值放入字节
。
It is giving that error message because you are attempting to assign the value of an int-valued expression to a byte
. That is a potentially lossy operation: intuitively, you cannot put all possible int
values into a byte
.
当您将一个int值表达式赋给a语言时,Java语言要求您使用(byte)
强制转换 byte
。
The Java language requires you to use a (byte)
cast when you assign an int-valued expression to a byte
.
您有两个地方可以这样做:
There are two places where you are doing this:
cryptedByte[i] = (data[i] + 13) % 256;
decryptedByte[i] = (data[i] * 256) - 13;
在第一个中,表达式的值在0到255范围内......但是Java byte
值在-128到+127之间。
In the first one, the value of the expression is in the range 0 to 255 ... but Java byte
values are in the range -128 to +127.
在第二个中,表达式值可能是的值在( - 128 * 256) - 13
到`(+127 * 256) - 13.显然不合适。
In the second one, the expression values potentially have values in the range (-128 * 256) - 13
to `(+127 * 256) - 13. That clearly won't fit.
但这实际上没有实际意义。即使你(一个聪明的人)可以证明表达式的范围适合到字节$,Java也不允许使用上述代码的变体C $ C>。 JLS禁止这样做。 (Java编译器不需要是一般定理证明器!!)
But that is actually moot. The Java does not allow variants of the above code even if you (a smart human being) can prove that the range of the expression would "fit" into a byte
. The JLS forbids this. (A Java compiler not required to be a general theorem prover!!)
仅情况,其中可以将int值表达式赋值给没有类型转换的字节
当表达式是编译时常量表达式 AND 时,表达式的实际值在 byte
。
The only situation where an int-valued expression can be assigned to a byte
without a type-cast is when the expression is a compile-time constant expression AND the actual value of the expression is in the range of byte
.
如果您有兴趣,请在 JLS 15.2分配上下文,其中说明:
If you are interested, this is specified in JLS 15.2 Assignment Contexts which states:
此外,如果表达式是byte,short,char或int类型的常量表达式(第15.28节):
In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int:
- 如果变量的类型是byte,short或char,则可以使用缩小的原语转换,并且常量表达式的值可以在变量的类型中表示。
(你需要追逐什么样的规格) ns通过常量表达式和缩小原始转换。我会留给那些感兴趣的人为自己做。)
(You need to chase down what the spec means by "constant expression" and "narrowing primitive conversion". I'll leave that for interested people to do for themselves.)
所以我会在13和256前加一个(字节)强制转换?
so would i add a (byte) cast in front of the 13 and 256?
不。你需要抛出整个表达式;例如。
Nope. You need to cast the entire expression; e.g.
cryptedByte[i] = (byte) ((data[i] + 13) % 256);
这篇关于从int到byte的有损转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!