关于Java字节类型值大于256

about Java byte type value greater than 256

public void demo(byte[] p,byte[] buffer){

SecretKeySpec signingKey = new SecretKeySpec(p, "HmacSHA1");
int count = 4096;
int num = 1;



Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);

while(num < count){
    byte[] sig = new byte[4];
    sig[0] = (byte)num;


    mac.update(buffer,0,buffer.length);
    mac.update(sig,0,4);

    String result = byteArrayToHex(mac.doFinal());

    num ++;
}}

如果 num 值为 256.Will 则结果不正确

我该怎么办?

我不知道怎么做。

你问过:

Java byte type value greater than 256

无法存储高于 255 的值。

一个byte in Java is an octet, 8-bits. This is true of both the byte primitive type and the Byte包装器class。

作为有符号值,这意味着范围是 -128 到 127。作为无符号值,这意味着范围是 0 到 255。在这两种情况下都没有空间来表示 256 或更高的数字。

详见