binary – How can you represent a Huffman-tree in Hex?

I was trying to store a Huffman tree in a binary file recently. I thought it wouldn’t be a big problem since you actually have the binary codes already in front of you.

Well, the difference is that the file (in the hex editor) is logically coded in hex. So I tried to convert the “characters” from binary to hex. Without any success.

Let’s take this tree as an example:


My first attempt was to simply convert Binary to Hex. So for example 01 became 0x01. But there is a problem now: There are duplicates. So 01 as well as just 1 both became 0x01.

So I couldn’t use that. I also tried to just represent the zeros and ones in hex, for example: 10 1... (The equivalent of 1 01) but that wouldn’t be really efficient anymore.

So how could I now make a Huffman tree in Hex? Would I maybe even have to use a different encoding?

I hope I could describe my problem well enough.