int-packer
Pack integers into bigger integers
Usage
Say you want to encode some data into an integer:
1 digit: sex (0 male 1 female)
4 digits: birth year
2 digits: birth month
2 digits: birth day
1 digit: eye color (0 brown 1 blue 2 hazel 3 green 4 other)
1 digit: hair color (0 brown 1 black 2 blond 3 red 4 other)
2 digits: height in inches
3 digits: weight
With int-packer
you can create a "packer" instance with an array of digits:
var packer = 1 4 2 2 1 1 2 3;// you can also do this:var packer = 1 4 2 2 1 1 2 3;
And pack values into a larger number. pack()
returns an instance of
justmoon/node-bignum, with a special
toString()
method that zero-pads the value.
var num = packer;console;// <BigNum 0198311170169200>var unpacked = packer;console;// [ 0, 1983, 11, 17, 0, 1, 69, 200 ]
Labels
You can also assign labels to each section:
var packer = label: 'sex' length: 1 label: 'year' length: 4 label: 'month' length: 2 label: 'day' length: 2 label: 'eye' length: 1 label: 'hair' length: 1 label: 'height' length: 2 label: 'weight' length: 3;var packed = packer;console;// <BigNum 0198311170169200>var unpacked = packer;console;/*{ sex: 0, year: 1983, month: 11, day: 17, eye: 0, hair: 1, height: 69, weight: 200}*/
Signed ints
You can pack signed ints by using negative numbers to define the packer. Note that an extra digit will be needed to store the sign.
var packer = -1 -3 -4 1;var packed = packer;console;// <BigNum 011800099991>var unpacked = packer;console;// [ -1, 800, -9999, 1 ]- - - ### Developed by Terra Eclipsehttp://www.terraeclipse.com)Terra Eclipse Inc is a nationally recognized political technology andstrategy firm located in Aptos CA and Washington DC - - - ### License: MIT - 2013 Carlos