Last active
December 7, 2015 17:18
-
-
Save leodutra/507134ce06110223af06 to your computer and use it in GitHub Desktop.
JavaScript decimal, hexadecimal, octal, binary manipulation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function baseTobase(n, base1, base2) { | |
return parseInt(n, base1).toString(base2); | |
} | |
function decToHex(n) { | |
// more about >>> for negatives | |
// here: http://stackoverflow.com/a/17106974/1260526 | |
// here: https://en.wikipedia.org/wiki/Two%27s_complement | |
// and here: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_shift_operators | |
return (n >>> 0).toString(16); | |
} | |
function hexToDec(n) { | |
return parseInt(n, 16); | |
} | |
function hexToBin(n) { | |
return parseInt(n, 10).toString(2); | |
} | |
function decToBin(n) { | |
return n.toString(2); | |
} | |
function binToDec(n) { | |
return parseInt(n, 2); | |
} | |
function binToHex(n) { | |
return parseInt(n, 2).toString(16); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment