Question: Problem of parsing binary number

This is my source code to perform several binary arithmetic and HUffman Compression

Freq := proc (s) options operator, arrow; map(proc (x) options operator, arrow; rhs(x) = lhs(x) end proc, {StringTools:-CharacterFrequencies(s)}) end proc; s := readstat("x will be assigned "); Fr := Freq(s);

algo
{1 = "a", 1 = "g", 1 = "l", 1 = "o"}

HuffmanTree := proc (s) options operator, arrow; if nops(s) <= 1 then rhs(s[]) else procname(`union`(s[3 .. -1], {lhs(s[1])+lhs(s[2]) = [rhs(s[1]), rhs(s[2])]})) end if end proc; HT := HuffmanTree(Fr);


[["a", "g"], ["l", "o"]]

HuffmanCoding := proc (s, p := "") if s::string then s = p else procname(s[1], cat(p, 0)), procname(s[2], cat(p, 1)) end if end proc; HC := HuffmanCoding(HT);

"a" = "00", "g" = "01", "l" = "10", "o" = "11"

C := table([HC]); b := cat(map(proc (x) options operator, arrow; C[x] end proc, StringTools:-Explode(s))[]);

"00100111"

# Begin the procedure with make string to integer
z := parse(b);

                                                                   100111    #Should be 00100111 after parsing

# Step 1:add 1 to z
binaryAdd := proc (z, y) options operator, arrow; convert(convert(z, decimal, 2)+convert(y, decimal, 2), binary) end proc;
z1 := binaryAdd(z, 1);


101000

# Step 2: Reverse the number from left to right
z2 := convert(z1, base, 10);

[0, 0, 0, 1, 0, 1]


z3 := (parse@cat@op)(z2);


                                                                101  #Should be 000101 after parsing

 # Step 3: Addition between the first secret key and the Huffman Code

randomize();

p := rand(0 .. 2^(length(z3)-2)):

rn := convert(p(), binary);

1

z4 := binaryAdd(z3, rn);

110

# Step 4:Obtain 2's complement

s := length(z4);

z5 := convert(z4, decimal, binary);

6

z6 := [Bits:-GetBits(-z5, -1 .. 0, bits = s)];

[0, 1, 0]

z7 :=(parse@cat@op)(z6);

                                                       10  # Should be 010 after passing

 

The Bold part is MAPLE answer. My problem for this MAPLE source code is the underlined answer  as I can't get the answer I need after I have my parsing of string or list. Do this problem can be solved or it is impossible to make the leading zero all the way of the process. I try to figure it out for several solution but it still the same results. Thus hope some experts or someone can help me solve it together. Appreciated if get solved.

Please Wait...