How to convert a byte array (MD5 hash) into a string (36 chars)?

后端 未结 7 2049
走了就别回头了
走了就别回头了 2021-01-14 01:18

I\'ve got a byte array that was created using a hash function. I would like to convert this array into a string. So far so good, it will give me hexadecimal string.

7条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-14 01:54

    Usually a power of 2 is used - that way one character maps to a fixed number of bits. An alphabet of 32 bits for instance would map to 5 bits. The only challenge in that case is how to deserialize variable-length strings.

    For 36 bits you could treat the data as a large number, and then:

    • divide by 36
    • add the remainder as character to your result
    • repeat until the division results in 0

    Easier said than done perhaps.

提交回复
热议问题