Compress 21 Alphanumeric Characters in to 16 Bytes

后端 未结 8 714
耶瑟儿~
耶瑟儿~ 2020-12-16 15:07

I\'m trying to take 21 bytes of data which uniquely identifies a trade and store it in a 16 byte char array. I\'m having trouble coming up with the right algor

8条回答
  •  抹茶落季
    2020-12-16 15:15

    You can do this in ~~15bytes (14 bytes and 6 bits).

    For each character from trace_num_ you can save 1 bit if you want save ascii in 7 bits.

    • Then you have 2 bytes free and 2 bits, you must have 5.

    Let get number information, each char can be one from ten values (0 to 9). Then you must have 4 bits to save this character, to save number you must have 1 byte and 4 bits, then you save half of this.

    • Now you have 3 bytes free and 6 bits, you must have 5.

    If you want to use only qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM1234567890[] You can save each char in 6 bits. Then you have next 2 bytes and 2 bits.

    • Now you have 6 bytes left, and your string can save in 15 bytes + nulltermination = 16bytes.

    And if you save your number in integer on 10 bytes. You can fit this into 14 bytes and 6 bits.

提交回复
热议问题