Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

biguint-format

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

biguint-format

Node.js module to format big uint numbers from a node Buffer, an array of bytes or a string in hex format to decimal, hexadecimal or octet string

  • 0.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4.4K
decreased by-42.46%
Maintainers
1
Weekly downloads
 
Created
Source

JavaScript uses IEEE 754 double-precision floats 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

Problem

Presentation of small integer in decimal format works fine (e.g. 0x1FF). However, we can see an issue when try to convert big integers like 0x1234567890abcdeffedcba908765421 to string decimal.

(0x1FF).toString(10) // returns '511' - correct
(0x1234567890abcdeffedcba908765421).toString(10) // returns '1.5123660750094533e+36' - incorrect - lose integer precision

Solution

Node.js biguint-format module has been built in order to help display very large unsigned integers without any integer precision lose. biguint-format takes an array of bytes (values from 0 to 255) or node Buffer and converts it to decimal format.

var biguint  = require('biguint-format');

// 0x1234567890abcdeffedcba908765421 split into bytes
biguint.toDecimalString([0x1, 0x23, 0x45, 0x67, 0x89, 0x0a, 0xbc, 0xde, 0xff, 0xed, 0xcb, 0xa9, 0x08, 0x76, 0x54, 0x21])

// output value is '1512366075009453296626403467035300897' - no integer precision lose

biguint-format can also take array of bytes in Big Endian (BE - default value) and Little Endian (LE) formats. Check wikipedia for more details.

var biguint  = require('biguint-format');

var buffer1 = new Buffer([0x63, 0xA7, 0x27]);
var buffer2 = new Buffer([0x27, 0xA7, 0x63]);

biguint.toDecimalString(buffer1, 'LE') // returns '2598755'
biguint.toDecimalString(buffer2, 'BE') // returns '2598755'
biguint.toDecimalString(buffer2)       // returns '2598755'

Keywords

FAQs

Package last updated on 07 Jan 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc