简体   繁体   中英

Java convert unsigned big integer to unsigned byte

How to convert unsigned BigInteger into unsigned byte when the value is higher?

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

How to get unsigned byte value from BigInteger?

you should cast "202" to int because java does not have unsigned byte . I suppose .intValue() would help

Maybe you looking for something like this:

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;
  }
}

and here is a very good explanation: Can we make unsigned byte in Java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM