0.1.12 • Published 7 years ago

sffc-encoder v0.1.12

Weekly downloads
16
License
Apache-2.0
Repository
github
Last release
7 years ago

sffc-encoder

Build Status Coverage Status npm version npm version

js-standard-style

sffc-encoder provides the encode/decode functions to and from SFFC (Significant Figures First Code) format.

SFFC format is an Integer to binary encoding format with a changing byte size (full bytes) based on the number of significant digits in base 10 that are needed to represent the Integer. This format benefits the more "rounded" (Higher powers of 10) Integer with a lower byte size encoding.

The decoding Scheme:

  • First 2 or 3 bits are length flag:
  1. If first 2 bits are 1 then the flag is 2 bits.
  2. Else the flag is the first 3 bits.
  • Remove the flag bits and concat the rest with the next extra amount of bytes using the conversion table (appendix A).
  • Split the bits to mantis and exponent to their respective bits using table 1.
  • Multiply the mantis by 10^exponent in base 10 to get the encoded Integer.

There is also some encoding optimization, for example, even though 23 has 2 significant figures we can still encode it using 3 flag bits and 5 binary bits in only 1 byte.

Installation

$ npm install sffc-encoder

Encode

Params:

  • number - takes any integer between 0 to 9007199254740991 (Number.MAX_SAFE_INTEGER)

Returns a new Buffer holding the encoded Integer

var sffc = require('sffc-encoder')
console.log(sffc.encode(1321321321)) // Will print: <Buffer 82 76 0e 1b 48>
console.log(sffc.encode(1)) // Will print: <Buffer 01>

Decode

Params:

  • consume - takes a consumable buffer (You can use buffer-consumer like in the example to create one)

Returns the Integer as number

var sffc = require('sffc-encoder')
var consumer = require('buffer-consumer')

var codeBuffer = new Buffer([0x82,0x76,0x0e,0x1b,0x48])

console.log(sffc.decode(consumer(codeBuffer))) // Will print: 1321321321

Testing

In order to test you need to install mocha globaly on your machine

$ cd /"module-path"/sffc-encoder
$ mocha

Apendix A


Byte FlagSignificant DigitsMantis bit sizeExponent bit sizeExtra BytesTotal Bytes
0001-31 (Number Value, not significant digits)5001
00129412
010517423
011725434
1001034345
1011242356
11*1654067

License

Apache-2.0