Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tuurdutoit/number-convert
A function for converting numbers to and from different bases w/o loss of precision.
https://github.com/tuurdutoit/number-convert
Last synced: 10 days ago
JSON representation
A function for converting numbers to and from different bases w/o loss of precision.
- Host: GitHub
- URL: https://github.com/tuurdutoit/number-convert
- Owner: TuurDutoit
- Created: 2016-03-18T21:42:09.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-03-18T21:42:47.000Z (almost 9 years ago)
- Last Synced: 2024-12-15T17:23:17.581Z (about 1 month ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
A function for converting numbers to and from different bases w/o loss of precision.
===Adapted from [http://www.danvk.org/hex2dec.html](http://www.danvk.org/hex2dec.html)
The problem is that parseInt("0x12345...") isn't precise enough to convert
big integers correctly. This uses strings as input and, internally, arrays as workhorses.## API
This module exports a function, `convertBase` (this function __is__ the module export), with a few shorthand methods (as properties of the exported function).### convertBase(string input, number fromBase, number toBase) : string
Converts a string representation of a number in base `fromBase` and converts it into a string respresentation in base `toBase`. That's it.### hexToDec(string input)
Shorthand for `convertBase(input, 16, 10)`.### decToHex(string input)
Shorthand for `convertBase(input, 10, 16)`.### decToBin(string input)
Shorthand for `convertBase(input, 10, 2)`.### binToDec(string input)
Shorthand for `convertBase(input, 2, 10)`.### hexToBin(string input)
Shorthand for `convertBase(input, 16, 2)`.### binToHex(string input)
Shorthand for `convertBase(input, 2, 16)`.