UNPKG

6.18 kBMarkdownView Raw
1Big UInteger Formatter
2====
3[![Build Status](https://travis-ci.org/T-PWK/biguint-format.svg?branch=master)](https://travis-ci.org/T-PWK/biguint-format)
4[![npm version](https://badge.fury.io/js/biguint-format.svg)](http://badge.fury.io/js/biguint-format)
5[![Coverage Status](https://coveralls.io/repos/github/T-PWK/biguint-format/badge.svg?branch=master)](https://coveralls.io/github/T-PWK/biguint-format?branch=master)
6[![Code Climate](https://codeclimate.com/github/T-PWK/biguint-format/badges/gpa.svg)](https://codeclimate.com/github/T-PWK/biguint-format)
7
8An arbitrary length unsigned integer formatter library for Node.js.
9
10JavaScript uses [IEEE 754 double-precision floats](http://en.wikipedia.org/wiki/IEEE_floating_point) to represents numbers. That works perfectly fine for small numbers, however, it is an issue for big integers. This means they lose integer precision for values beyond `+/- 2 pow 53`
11
12### Problem ###
13
14Presentation of *small* integer in decimal format works fine (e.g. `0x1FF`). However, we can see an issue when we try to convert big integers like `0x1234567890abcdeffedcba908765421` to string decimal.
15
16```js
17(0x1FF).toString(10) // returns '511' - correct
18(0x1234567890abcdeffedcba908765421).toString(10)
19
20// output is '1.5123660750094533e+36' - incorrect - lose integer precision
21```
22
23### Solution ###
24
25Node.js `biguint-format` module has been built in order to help display very large (arbitrary length) unsigned integers without any integer precision lose.
26
27Example:
28```js
29var format = require('biguint-format');
30
31// 0x1234567890abcdeffedcba908765421 split into bytes
32format([
33 0x1, 0x23, 0x45, 0x67, 0x89, 0x0a, 0xbc, 0xde,
34 0xff, 0xed, 0xcb, 0xa9, 0x08, 0x76, 0x54, 0x21], 'dec')
35
36// output value is '1512366075009453296626403467035300897' - no integer precision lose
37```
38## Installation ##
39```
40$ npm install biguint-format
41```
42
43## API ##
44
45The `biguint-format` module is a function (`fn(number, format [, options])`) which performs number conversion to the required string format.
46
47The `number` argument represents an arbitrary length unsigned integer number to be converted to string. It can be provided in one of the following formats:
48* Node.js [Buffer](http://nodejs.org/api/buffer.html) e.g. `new Buffer([0x1, 0xFF])`
49* An array of bytes (values from `0x00` to `0xFF`) e.g. `[0x1, 0xFF]`.
50* A string with a number in a hexadecimal format e.g. `0x1FF0A` or `1FF0A`
51
52The `format` argument represents output string format and it can be one of the following options:
53* `dec` - conversion to decimal format e.g. `123324884`
54* `bin` - conversion to binary format e.g. `1100101010`
55* `hex` - conversion to hexadecimal format e.g. `0xADFFAA11`
56* `oct` - conversion to octet format e.g. `07771`
57
58If `format` argument is missing, `dec` format is used as a default option. Note that you will have to specify format if you would like to use `options` argument.
59
60The `options` argument (optional) is an object which provides some additional conversion details:
61* `format` - specifies format of the input number. It can be either `BE` for Big Endian or `LE` for Little Endian. `BE` is a default value. Check [wikipedia](http://en.wikipedia.org/wiki/Endianness) for more details.
62* `prefix` - output string prefix. Note that this option is not supported by `dec` conversion.
63* `groupsize` - splits output string into groups of `groupsize` length characters.
64* `delimiter` - specifies delimiter string to be inserted in between character groups. Default value is space. It is quite handy option when dealing with large numbers.
65* `trim` - (works only with `bin` formatting) specifies if the leading 0's should be trimmed.
66* `padstr` - string used for right-padding of the formatted string if its length (including prefix and grouping) is less than value of `size` parameter.
67* `size` - determines formatted string size. That option has effect only with `padstr` option. Note that the formatted string is not trimmed if its length is longer than value of `size` parameter.
68
69### Examples ###
70
71```js
72var format = require('biguint-format');
73
74var buffer1 = new Buffer([0x63, 0xA7, 0x27]);
75var buffer2 = new Buffer([0x27, 0xA7, 0x63]);
76
77format(buffer1, 'dec', {format:'LE'}) // returns '2598755'
78format(buffer2, 'dec', {format:'BE'}) // returns '2598755'
79format(buffer2, 'dec') // returns '2598755'
80
81format(buffer1, 'hex', {format:'LE'}) // returns '27a763'
82format(buffer2, 'hex', {format:'BE'}) // returns '27a763'
83format(buffer2, 'hex', {prefix:'0x'}) // returns '0x27a763'
84
85format(buffer2, 'bin') // 001001111010011101100011
86format(buffer2, 'bin', {groupsize:8}) // 00100111 10100111 01100011
87format(buffer2, 'oct') // 11723543
88format(buffer2, 'oct', {prefix:'0'}) // 011723543
89```
90
91Use of `delimiter` option which helps with large numbers e.g.
92```js
93var format = require('biguint-format');
94
95format([0x2A, 0xFF, 0x1E, 0x22, 0x11, 0x30, 0x12, 0x2F], 'bin')
96format([0x2A, 0xFF, 0x1E, 0x22, 0x11, 0x30, 0x12, 0x2F], 'bin', {groupsize:8})
97
98// returned values
990010101011111111000111100010001000010001001100000001001000101111 // no delimiter
10000101010 11111111 00011110 00100010 00010001 00110000 00010010 00101111 // with delimiter
101```
102
103Example of `trim` option which works only with binary formatter
104```js
105var format = require('biguint-format');
106var buffer = new Buffer([0x1, 0xA7, 0x63]);
107
108format(buffer, 'bin'); // returns 000000011010011101100011
109format(buffer, 'bin', {trim:true}); // returns 11010011101100011
110```
111
112Example of `padstr` and `size` options
113```js
114var format = require('biguint-format');
115var buffer = new Buffer([0x1, 0xA7, 0x63]);
116
117format(buffer, 'dec'); // returns 108387
118format(buffer, 'oct'); // returns 323543
119format(buffer, 'hex'); // returns 1a763
120
121format(buffer, 'dec', {padstr:'0', size:6}); // returns 108387 - no padding effect
122format(buffer, 'oct', {padstr:'0', size:10}); // returns 0000323543
123format(buffer, 'hex', {padstr:'0', size:6}); // returns 01a763
124```
125
126## Author ##
127Written by Tom Pawlak - [Blog](https://blog.abelotech.com)
128
129## License ##
130
131Copyright (c) 2014 Tom Pawlak
132
133MIT License : https://blog.abelotech.com/mit-license/