JSFiddle - React, Tailwind, and code Playground
by TheBuzzer
HTML
<fieldset>
<input type="text" id="Txt1" value="" />
<input type="text" id="Txt2" value="" />
<input type="button" onclick="$('#Txt2').val(Base.encode($('#Txt1').val()))" value="Do It!" />
</fieldset>
JavaScript
var Base128 = new Class({
initialize: function() {
}
});
Base128.encode = function(value) {
if(!is_integer(value) || value < 0) { throw new Base128_Exception("value must be unsigned integer");}
if(value <= 127) {
return chr(value);
}
result = "";
bin = decbin(value);
bit8 = "1";
index = strlen(bin);
substrLenght = 7;
while (0 < index) {
if(index < 8) {
bit8 = "0";
}
index = index - 7;
if(index < 0) { substrLenght = substrLenght + index; index = 0; }
bin7bit = substr(bin, index, substrLenght);
dec = bindec(bit8 + bin7bit);
char = chr(dec);
result = result + char;
}
return result;
};
Base128.encodeToWriter = function(value)
{
var writer = arguments.length >= 1 ? arguments[0] : IO_Writer_Interface;
writer.writeBytes(Base128.encode(value));
};
Base128.decodeFromReader = function()
{
var reader = arguments.length >= 1 ? arguments[0] : IO_Reader_Interface;
continue = true;
result = "";
while (continue) {
byte = unpack("C", reader.getByte());
bin = sprintf("%b", byte[1]);
if(strlen(bin) < 8) { continue = false; }
//if(!string && $continue) { throw new Base128_Exception("expecting next byte"); }
bin = str_pad(bin, 8, "0", STR_PAD_LEFT);
bin7bit = substr(bin, 1, 7);
result = bin7bit + result;
}
return bindec(result);
};