Compress 21 Alphanumeric Characters in to 16 Bytes

后端 未结 8 680
耶瑟儿~
耶瑟儿~ 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:36

    If it can only contain letters, then you have less than 64 possibilities per character (26 upper case, 26 lower case, leaving you 12 for space, terminator, underscore, etc). With 6 bits per character, you should get there - in 15 characters. Assuming you don't support special characters.

    0 讨论(0)
  • 2020-12-16 15:36

    Use the first 10 bits for the 3-character numeric string (encode the bits like they represent a number and then pad with zeros as appropriate when decoding).

    Okay, this leaves you with 118 bits and 16 alphanumeric characters to store.

    0x00 to 0x7F (if you mean inclusive) comprises 128 possible characters to represent. That means that each character can be identified by a combination of 7 bits. Come up with an index mapping each number those 7 bits can represent to the actual character. To represent 16 of your "alphanumeric" characters in this way, you need a total of 112 bits.

    We now have 122 bits (or 15.25 bytes) representing our data. Add an easter egg to fill in the remaining unused bits and you have your 16 character array.

    0 讨论(0)
提交回复
热议问题