How to resolve the algorithm Variable-length quantity step by step in the Icon and Unicon programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Variable-length quantity step by step in the Icon and Unicon programming language
Table of Contents
Problem Statement
Implement some operations on variable-length quantities, at least including conversions from a normal number in the language to the binary representation of the variable-length quantity for that number, and vice versa. Any variants are acceptable.
With above operations,
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Variable-length quantity step by step in the Icon and Unicon programming language
Source code in the icon programming language
procedure main()
every i := 2097152 | 2097151 | 1 | 127 | 128 | 589723405834 | 165 | 256 do
write(image(i)," = ",string2hex(v := uint2vlq(i))," = ",vlq2uint(v))
end
procedure vlq2uint(s) #: decode a variable length quantity
if *s > 0 then {
i := 0
s ? while h := ord(move(1)) do {
if (pos(0) & h > 128) | (not pos(0) & h < 128) then fail
i := 128 * i + h % 128
}
return i
}
end
procedure uint2vlq(i,c) #: encode a whole number as a variable length quantity
if "integer" == type(-1 < i) then
return if i = 0 then
char((/c := 0)) | ""
else
uint2vlq(i/128,1) || char((i % 128) + ((/c := 0) | 128) )
end
procedure string2hex(s) #: convert a string to hex
h := ""
every i := ord(!s) do
h ||:= "0123456789abcdef"[i/16+1] || "0123456789abcdef"[i%16+1]
return h
end
You may also check:How to resolve the algorithm UPC step by step in the 11l programming language
You may also check:How to resolve the algorithm Pangram checker step by step in the Ada programming language
You may also check:How to resolve the algorithm Delete a file step by step in the Pascal programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Send email step by step in the Fortran programming language