How to Convert Int to Unsigned Byte and Back

前端 未结 10 678
死守一世寂寞
死守一世寂寞 2020-11-28 23:04

I need to convert a number into an unsigned byte. The number is always less than or equal to 255, and so it will fit in one byte.

I also need to convert that byte ba

10条回答
  •  猫巷女王i
    2020-11-28 23:41

    Handling bytes and unsigned integers with BigInteger:

    byte[] b = ...                    // your integer in big-endian
    BigInteger ui = new BigInteger(b) // let BigInteger do the work
    int i = ui.intValue()             // unsigned value assigned to i
    

提交回复
热议问题