从 int 到 byte 的有损转换? [英] lossy conversion from int to byte?

查看:22
本文介绍了从 int 到 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 值放入 byte.

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.

Java 语言要求您在将 int 值表达式分配给 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 不允许上述代码的变体,即使您(一个聪明人)可以证明表达式的范围适合"成一个 byte.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 值表达式可以分配给 byte 而不进行类型转换是当表达式是编译时常量表达式 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,并且常量表达式的值可以在变量的类型中表示,则可以使用收缩原语转换.

(您需要了解规范中常量表达式"和缩小原始转换"的含义.我将把它留给感兴趣的人自己做.)

(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屋!

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