byte[] to unsigned BigInteger?

后端 未结 6 790
逝去的感伤
逝去的感伤 2020-12-06 09:21

Motivation: I would like to convert hashes (MD5/SHA1 etc) into decimal integers for the purpose of making barcodes in Code128C. For simplicity, I prefer al

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 09:57

    Examining the documentation for the relevant BigInteger constructor, we see:

    The individual bytes in the value array should be in little-endian order, from lowest-order byte to highest-order byte

    [...]

    The constructor expects positive values in the byte array to use sign-and-magnitude representation, and negative values to use two's complement representation. In other words, if the highest-order bit of the highest-order byte in value is set, the resulting BigInteger value is negative. Depending on the source of the byte array, this may cause a positive value to be misinterpreted as a negative value.

    [...]

    To prevent positive values from being misinterpreted as negative values, you can add a zero-byte value to the end of the array.

提交回复
热议问题