Java将无符号大整数转换为无符号字节

时间:2018-11-02 12:22:07

标签: java bit biginteger unsigned

当值较高时如何将无符号BigInteger转换为无符号字节?

String value = "202";
BigInteger a = new BigInteger( value );
Byte b = new BigInteger( value ).byteValue() // returns -54

如何从BigInteger获取无符号字节值?

2 个答案:

答案 0 :(得分:1)

您应将“ 202”强制转换为int,因为Java没有unsigned byte。 我想.intValue()会有所帮助

答案 1 :(得分:1)

也许您正在寻找这样的东西:

import java.math.*;
public class MyClass {
    public static void main(String args[]) {
        String value = "202";
        BigInteger a = new BigInteger( value );
        Byte b = new BigInteger( value ).byteValue() ;
        System.out.println(a);
        System.out.println(unsignedToBytes((byte)b));
    }

    public static int unsignedToBytes(byte b) {
    return (int)b & 0xFF;
  }
}

这是一个很好的解释: Can we make unsigned byte in Java