Skip to content

Sparebola/uleb128_33

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ULEB128_33

Encoding and decoding large numbers (uleb128 and uleb128_33)

Installation

npm install uleb128_33

Documentation

readUleb128: Gets the ArrayLike parameter, returns the object. You can also specify an index from which to read in the buffer, the default is 0.

const readUleb128: (buffer: ArrayLike<number>, index?: number) => {
    value: number;
    length: number;
};

readUleb128_33: Gets the ArrayLike parameter, returns the object. You can also specify an index from which to read in the buffer, the default is 0.

const readUleb128_33: (buffer: ArrayLike<number>, index?: number) => {
    value: number;
    length: number;
    isMark: number;
};

writeUleb128: Gets an integer parameter, can be a string or a number. Returns array of bytes

 const writeUleb128: (integer: number | string) => number[];

writeUleb128_33: Gets an integer parameter, can be a string or a number, and an optional isMark, boolean (see below). If not specified, it is determined automatically. Returns an array of bytes

const writeUleb128_33: (integer: number | string, isMark?: boolean) => number[];

getUleb128Length: Gets an integer parameter, can be a string or a number. Returns number length

const getUleb128Length: (integer: number | string) => number;

getUleb128_33Length: Gets an integer parameter, can be a string or a number. Returns number length

const getUleb128_33Length: (integer: number | string) => number;
isMark

If the "isMark" is 0, then the value read is a number (32-bit number) Otherwise, the value is a 64-bit number, and you need to read another ULEB128

Usage

ESNext

import {
  getUleb128Length,
  getUleb128_33Length,
  readUleb128,
  readUleb128_33,
  writeUleb128,
  writeUleb128_33,
} from "uleb128_33";

CommonJS

const {
  getUleb128Length,
  getUleb128_33Length,
  readUleb128,
  readUleb128_33,
  writeUleb128,
  writeUleb128_33,
} = require("uleb128_33");

readUleb128

const uintArray = new Uint8Array([
  133, 133, 133, 5, 255, 243, 151, 229, 228, 131, 24, 11, 35, 11, 111, 66,
]);
const dataOne = readUleb128(uintArray);
const dataTwo = readUleb128(uintArray.subarray(dataOne.length));
console.log(dataOne); // { length: 4, value: 10568325 }
console.log(dataTwo); // { length: 7, value: 105683251231231 }

readUleb128_33

const uintArray = [
  138, 152, 184, 128, 1, 193, 253, 243, 174, 137, 188, 188, 222, 3,
];
const dataOne = readUleb128_33(uintArray);
const dataTwo = readUleb128_33(uintArray.slice(dataOne.length));
console.log(dataOne); // { isMark: 0, value: 134678021, length: 5 }
console.log(dataTwo); // { isMark: 1, value: 134678012312321888, length: 9 }

writeUleb128_33

const dataOne = writeUleb128_33(134678021);
const dataTwo = writeUleb128_33("134678012312321888");
console.log(dataOne); // [138, 152, 184, 128, 1]
console.log(dataTwo); // [193, 253, 243, 174, 137, 188, 188, 222, 3]

Test

You can run the tests through npm run test. You will need to install rimraf, typescript, and jest in the global area

LICENSE

The MIT License (MIT)

About

Encoding and decoding large numbers (uleb128 and uleb128_33)

Resources

Stars

Watchers

Forks