Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:正确的铸造方法;“未签名”;字节到整数_Java_Int_Byte - Fatal编程技术网

Java:正确的铸造方法;“未签名”;字节到整数

Java:正确的铸造方法;“未签名”;字节到整数,java,int,byte,Java,Int,Byte,我已经花了好几个小时在网上找虫子了。有几个bug,但其中之一是: /** * @param IV An array of 8 bytes */ public void setupIV(final byte[] IV) { short[] sIV = new short[IV.length>>1]; for(int i=0;i<sIV.length;++i) { sIV[i] = (short)((IV[i << 1] <&l

我已经花了好几个小时在网上找虫子了。有几个bug,但其中之一是:

/**
 * @param IV An array of 8 bytes
 */
public void setupIV(final byte[] IV) {
    short[] sIV = new short[IV.length>>1];
    for(int i=0;i<sIV.length;++i) {
        sIV[i] = (short)((IV[i << 1] << 8) | IV[(2 << 1) + 1]);
    }
    setupIV(sIV);
}

它是有效的,但我想知道这是否是正确的方式来做我需要的?这个解决方案似乎有些愚蠢。

我不确定这会有多大帮助,但您可以通过使用
0xFF
运行二进制AND操作,将有符号字节转换为无符号值

进一步考虑此逻辑,您可以在强制转换后使用适当的操作数在int上运行类似的AND来检查溢出。当然,这假设您总是期望正值或零值数字,或者换句话说,无符号数字


(short)((IV[i使用库函数进行此操作(在类中)。您将能够控制endianness作为奖励。这将取决于Java创建者的效率

package tests.StackOverflow.q20776371;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ShortBuffer;

public class q20776371 {


    public static void main(String[] args) {

        byte[] bb = { (byte)0xFF, (byte)0x01, (byte)0x02, (byte)0x03 };

        ByteBuffer buffer1 = ByteBuffer.wrap(bb);

        // set endianness
        // buffer1.order(ByteOrder.BIG_ENDIAN);
        // buffer1.order(ByteOrder.LITTLE_ENDIAN);

        ShortBuffer buffer2 = buffer1.asShortBuffer();

        short[] ss = new short[bb.length>>1];
        buffer2.get(ss);

        for(int i=0; i<ss.length; ++i) {
            System.out.println(String.format("%04x", ss[i]));
        }
    }


}
package tests.StackOverflow.q20776371;
导入java.nio.ByteBuffer;
导入java.nio.ByteOrder;
导入java.nio.ShortBuffer;
公共类q20776371{
公共静态void main(字符串[]args){
字节[]bb={(字节)0xFF,(字节)0x01,(字节)0x02,(字节)0x03};
ByteBuffer1=ByteBuffer.wrap(bb);
//集端性
//buffer1.顺序(ByteOrder.BIG_ENDIAN);
//buffer1.顺序(ByteOrder.LITTLE_ENDIAN);
ShortBuffer2=buffer1.asShortBuffer();
short[]ss=新的short[bb.length>>1];
buffer2.get(ss);

对于(int i=0;i@Darkhogg:该注释具有误导性:不需要强制转换为
int
,因为
&
将其操作数提升为
int
(或
long
,如果其中一个参数属于该类型)。因此表达式
(b&0xFF)
的类型为
int
,并生成“无符号”值。当然,不应该将此值转换回
字节,因为这会使其再次“有符号”。@meriton我个人喜欢将
字节
显式转换为
int
,因为很容易忘记隐式转换是在哪里添加的,并且清楚地说明了意图。将其组合在一起的表达式应该是
(短)((IV[i
IV[(2@Jan)我认为它实际上应该由
IV[(i
int result=(int)unsignedByte)&0xFF;
package tests.StackOverflow.q20776371;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ShortBuffer;

public class q20776371 {


    public static void main(String[] args) {

        byte[] bb = { (byte)0xFF, (byte)0x01, (byte)0x02, (byte)0x03 };

        ByteBuffer buffer1 = ByteBuffer.wrap(bb);

        // set endianness
        // buffer1.order(ByteOrder.BIG_ENDIAN);
        // buffer1.order(ByteOrder.LITTLE_ENDIAN);

        ShortBuffer buffer2 = buffer1.asShortBuffer();

        short[] ss = new short[bb.length>>1];
        buffer2.get(ss);

        for(int i=0; i<ss.length; ++i) {
            System.out.println(String.format("%04x", ss[i]));
        }
    }


}