Skip to content

Instantly share code, notes, and snippets.

@reactima
Forked from andheiberg/baseConverter.js
Created June 17, 2018 13:33
Show Gist options
  • Save reactima/bd101dfa2013ceef5bb515a4e146b0f8 to your computer and use it in GitHub Desktop.
Save reactima/bd101dfa2013ceef5bb515a4e146b0f8 to your computer and use it in GitHub Desktop.
Convert From/To Binary/Decimal/Hexadecimal in JavaScript
/**
* Convert From/To Binary/Decimal/Hexadecimal in JavaScript
* https://gist.github.com/faisalman
* https://gist.github.com/AndreasHeiberg
*
* Copyright 2012-2015, Faisalman <fyzlman@gmail.com>
* Licensed under The MIT License
* http://www.opensource.org/licenses/mit-license
*/
(function(){
var ConvertBase = {
data: {},
subject: function(subject) {
this.data.subject = subject;
return this;
},
from: function (baseFrom) {
this.data.baseFrom = baseFrom;
return this;
},
to: function(baseTo) {
this.data.baseTo = baseTo;
this.data.result = parseInt(this.data.subject, this.data.baseFrom).toString(this.data.baseTo);
return this;
},
result: function() {
return this.data.result;
},
// Could create function using bitwise operators, but life is too short :D
// I would much prefer working with more understandable datastructures and operators.
pad: function (bits) {
if (bits) {
var binPadded = "00000000000000000000000000000000" + this.data.result;
this.data.result = binPadded.substr(binPadded.length - bits);
}
return this;
},
// binary to decimal
bin2dec: function (num) {
return this.subject(num).from(2).to(10).result();
},
// binary to hexadecimal
bin2hex: function (num) {
return this.subject(num).from(2).to(16).result();
},
// decimal to binary
dec2bin: function (num, bits) {
return this.subject(num).from(10).to(2).pad(bits).result();
},
// decimal to hexadecimal
dec2hex: function (num) {
return this.subject(num).from(10).to(16).result();
},
// hexadecimal to binary
hex2bin: function (num, bits) {
return this.subject(num).from(16).to(2).pad(bits).result();
},
// hexadecimal to decimal
hex2dec: function (num) {
return this.subject(num).from(16).to(10).result();
},
upperAndLowerBit2dec: function (bits) {
// we get 2 bytes in an array
// convert the 2 bytes to hex bytes
hexUpper = this.dec2hex(bits[0]);
hexLower = this.dec2hex(bits[1]);
// now concatinate the two hex bytes fb + 74 = 0xfb74
// and convert that hexadecimal into a decimal
return this.hex2dec(hexUpper + hexLower);
},
upperAndLowerBit2float: function (bits, fraction) {
var number = this.upperAndLowerBit2dec(bits);
return this.unsignedIntToFloat(number, fraction);
},
unsignedIntToFloat: function (number, fraction) {
var signedNumber = this.unsignedToSignedInt(number);
return this.signedIntToFloat(signedNumber, fraction);
},
// Convert signed int to float using bit shifting of {fraction}
signedIntToFloat: function (number, fraction) {
return signedNumber / (1 << fraction);
},
unsignedToSignedInt: function(number) {
// if the number is desired as a float from an int with a binary point
// convert the unsigned int to a float with the binary point at the {fraction} bit
// convert from unsigned int to signed
var b = new ArrayBuffer(2);
var u = new Uint16Array(b);
var i = new Int16Array(b);
u[0] = number;
var signedNumber = i[0];
return signedNumber;
}
}
this.ConvertBase = ConvertBase;
})(this);
/*
* Usage example:
* ConvertBase.bin2dec('111'); // '7'
* ConvertBase.dec2hex('42'); // '2a'
* ConvertBase.hex2bin('f8'); // '11111000'
* ConvertBase.dec2bin('22'); // '10110'
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment