How to resolve the algorithm Variable-length quantity step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Variable-length quantity step by step in the Wren 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 Wren programming language

Source code in the wren programming language

import "/fmt" for Fmt, Conv
import "/str" for Str

var toOctets = Fn.new { |n|
    var s = Conv.itoa(n, 2)
    var le = s.count
    var r = le % 7
    var d = (le/7).floor
    if (r > 0) {
        d = d + 1
        s = Fmt.zfill(7 * d, s)
    }
    var chunks = Str.chunks(s, 7)
    var last = "0" + chunks[-1]
    s = chunks[0..-2].map { |ch| "1" + ch }.join() + last
    return Str.chunks(s, 8).map { |ch| Conv.atoi(ch, 2) }.toList
}

var fromOctets = Fn.new { |octets|
    var s = ""
    for (oct in octets) {
        var bin = Conv.itoa(oct, 2)
        bin = Fmt.zfill(7, bin)
        s = s + bin[-7..-1]
    }
    return Conv.atoi(s, 2)
}

var tests = [2097152, 2097151]
for (test in tests) {
    var octets = toOctets.call(test)
    var display = octets.map { |oct| "Ox" + Fmt.xz(2, oct) }.toList
    System.write("%(test) -> %(Fmt.v("s", 4, display, 0, " ", "")) -> ")
    System.print(fromOctets.call(octets))
}

  

You may also check:How to resolve the algorithm Haversine formula step by step in the smart BASIC programming language
You may also check:How to resolve the algorithm Trigonometric functions step by step in the SQL PL programming language
You may also check:How to resolve the algorithm Calendar - for REAL programmers step by step in the Vedit macro language programming language
You may also check:How to resolve the algorithm CRC-32 step by step in the RPL programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the RPL programming language