0

So basically I'm building a Huffman encoding. I already have a map for storing the letter and its corresponding representation

The map is declared as

map<char, string>

So it means I have:

<'A', 101>
<'S', 000011>
<'G', 01> 
...

Now, I need to encode a string, for example 'ASSGSSA', so I go to the map and get the value for each key. After that I need to WRITE the sequence of 1 and 0 (for example: 1010000011...) into a binary file but I don't know how to 'translate' each string into a bit/byte representation.

Could you please explain how can I make the transformation from string to bytes? and how to handle if the sequence hasn't size 8.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Nana89
  • 432
  • 4
  • 21

1 Answers1

0

Instead of mapping a character to a string, you should try mapping a character to an integral type, such as unsigned char.

static_assert(sizeof(unsigned char) == 1, "unsigned char is not 1 byte on this platform");
using byte = unsigned char;

#include <map>

using huffman_map_t = std::map<char, byte>;
huffman_map_t theMap = {{'A', 0x05},
                        {'S', 0x03},
                        ...
                        {'G', 0x01}};

In compilers that have implemented binary integer literals, you could get closer to representing the binary value in the code. I've also thrown in digit separaters to make the patterns a bit clearer. See Binary literals?, in particular, the answer by @sasha.sochka

huffman_map_t theMap = {{'A', 0b00000101},
                        {'S', 0b00000011},
                        ...
                        {'G', 0b00000001}};
Community
  • 1
  • 1
Michael Price
  • 8,088
  • 1
  • 17
  • 24