Last active
October 11, 2020 01:27
-
-
Save asmattic/2a447e1d78329de667b0b5ac6f963632 to your computer and use it in GitHub Desktop.
JS Bin// source https://jsbin.com/sezewos
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
function toHex(num) { | |
const base = 16; | |
const hexVals = '0123456789abcdef'; | |
let hex = []; | |
let sign = 1; | |
if(num === 0) return '0'; | |
if(num < 0) { | |
sign = -1; | |
num = -num; | |
} | |
while(num) { | |
let remainder = num % base; | |
num = (num - remainder) / base; | |
hex.push(hexVals[remainder]); | |
} | |
if(sign === 1) { | |
return hex.reverse().join(''); | |
} else { | |
let j = hex.length; | |
let carryDig = 1; | |
for(let i = 0; i < j; i++) { | |
if(hex[i] !== '0' || !carryDig) { | |
hex[i] = hexVals[base - 1 + carryDig - hexVals.indexOf(hex[i])]; | |
carryDig = 0 | |
} | |
} | |
// Check fo carry digit | |
if(carryDig) hex.push('1'); | |
} | |
return 'f'.repeat(8 - hex.length) + hex.reverse().join(''); | |
} | |
// Call the function | |
let number = 16777215; // ffffff | |
console.log(`Hex conversion number: ${number} => hex: ${toHex(number)}`); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function toHex(num) { | |
const base = 16; | |
const hexVals = '0123456789abcdef'; | |
let hex = []; | |
let sign = 1; | |
if(num === 0) return '0'; | |
if(num < 0) { | |
sign = -1; | |
num = -num; | |
} | |
while(num) { | |
let remainder = num % base; | |
num = (num - remainder) / base; | |
hex.push(hexVals[remainder]); | |
} | |
if(sign === 1) { | |
return hex.reverse().join(''); | |
} else { | |
let j = hex.length; | |
let carryDig = 1; | |
for(let i = 0; i < j; i++) { | |
if(hex[i] !== '0' || !carryDig) { | |
hex[i] = hexVals[base - 1 + carryDig - hexVals.indexOf(hex[i])]; | |
carryDig = 0 | |
} | |
} | |
// Check fo carry digit | |
if(carryDig) hex.push('1'); | |
} | |
return 'f'.repeat(8 - hex.length) + hex.reverse().join(''); | |
} | |
// Call the function | |
let number = 16777215; // ffffff | |
console.log(`Hex conversion number: ${number} => hex: ${toHex(number)}`);</script></body> | |
</html> |
This file contains hidden or 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 toHex(num) { | |
const base = 16; | |
const hexVals = '0123456789abcdef'; | |
let hex = []; | |
let sign = 1; | |
if(num === 0) return '0'; | |
if(num < 0) { | |
sign = -1; | |
num = -num; | |
} | |
while(num) { | |
let remainder = num % base; | |
num = (num - remainder) / base; | |
hex.push(hexVals[remainder]); | |
} | |
if(sign === 1) { | |
return hex.reverse().join(''); | |
} else { | |
let j = hex.length; | |
let carryDig = 1; | |
for(let i = 0; i < j; i++) { | |
if(hex[i] !== '0' || !carryDig) { | |
hex[i] = hexVals[base - 1 + carryDig - hexVals.indexOf(hex[i])]; | |
carryDig = 0 | |
} | |
} | |
// Check fo carry digit | |
if(carryDig) hex.push('1'); | |
} | |
return 'f'.repeat(8 - hex.length) + hex.reverse().join(''); | |
} | |
// Call the function | |
let number = 16777215; // ffffff | |
console.log(`Hex conversion number: ${number} => hex: ${toHex(number)}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment