Skip to content

Instantly share code, notes, and snippets.

@black-black-cat
Last active October 30, 2019 09:25
Show Gist options
  • Save black-black-cat/38a4f2a9a5e9bb7f2889c8c0a01db0e1 to your computer and use it in GitHub Desktop.
Save black-black-cat/38a4f2a9a5e9bb7f2889c8c0a01db0e1 to your computer and use it in GitHub Desktop.
convert base
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GistRun</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Hello world!</h1>
<script src="script.js"></script>
</body>
</html>
console.log('Hello World!');
(function(){
var ConvertBase = function (num) {
return {
from : function (baseFrom) {
return {
to : function (baseTo) {
return parseInt(num, baseFrom).toString(baseTo);
}
};
}
};
};
// binary to decimal
ConvertBase.bin2dec = function (num) {
return ConvertBase(num).from(2).to(10);
};
// binary to hexadecimal
ConvertBase.bin2hex = function (num) {
return ConvertBase(num).from(2).to(16);
};
// decimal to binary
ConvertBase.dec2bin = function (num) {
return ConvertBase(num).from(10).to(2);
};
// decimal to hexadecimal
ConvertBase.dec2hex = function (num) {
return ConvertBase(num).from(10).to(16);
};
// hexadecimal to binary
ConvertBase.hex2bin = function (num) {
return ConvertBase(num).from(16).to(2);
};
// hexadecimal to decimal
ConvertBase.hex2dec = function (num) {
return ConvertBase(num).from(16).to(10);
};
this.ConvertBase = ConvertBase;
})(this);
function decToHex(dec)
{
return parseInt(dec, 10).toString(16)
}
function hexToDec (hex) {
return parseInt(hex, 16).toString(10)
}
console.log('decToHex', decToHex(7829367))
console.log('decToHex', decToHex('-7829367.1234'))
console.log('hexToDec', hexToDec('777777'))
console.log('hexToDec', hexToDec('-777777.0997'))
/* todo: add styles */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment