function dec2hex(i){var result = "0000";if (i >= 0 && i <= 15) { result = "000" + i.toString(16); }else if (i >= 16 && i <= 255) { result = "00" + i.toString(16); }else if (i >= 256 && i <= 4095) { result = "0" + i.toString(16); }else if (i >= 4096 && i <= 65535) { result = i.toString(16); }return result}
function toHex(s){var re = new RegExp(/^\s*(\+|-)?((\d+(\.\d+)?)|(\.\d+))\s*$/);
if (re.test(s)) {return '#' + strHex( s.toString());}else {return 'A' + strHex(s);}}
/////////////////////////////////////////////////////////////////////////////// toHex(). Convert an ASCII string to hexadecimal./////////////////////////////////////////////////////////////////////////////toHex(s){if (s.substr(0,2).toLowerCase() == "0x") {return s;}
var l = "0123456789ABCDEF";var o = "";
if (typeof s != "string") {s = s.toString();}for (var i=0; i<s.length; i++) {var c = s.charCodeAt(i);
o = o + l.substr((c>>4),1) + l.substr((c & 0x0f),1);}
return "0x" + o;}
<?php
echo <<<EOD<html><head><title>Test</title><script>var a = -3.14159265;alert( "A = " + a );var b = a.toString();alert( "B = " + b );</script></head><body></body></html>EOD;
?>
function DecToHex(decimal) { // Data (decimal)
length = -1; // Base string lengthstring = ''; // Source 'string'
characters = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' ]; // character array
do { // Grab each nibble in reverse order because JavaScript has no unsigned left shift
string += characters[decimal & 0xF]; // Mask byte, get that character++length; // Increment to length of string
} while (decimal >>>= 4); // For next character shift right 4 bits, or break on 0
decimal += 'x'; // Convert that 0 into a hex prefix string -> '0x'
dodecimal += string[length];while (length--); // Flip string forwards, with the prefixed '0x'
return (decimal); // return (hexadecimal);}
/* Original: */
D = 3678; // Data (decimal)C = 0xF; // CheckA = D; // AccumulateB = -1; // Base string lengthS = ''; // Source 'string'H = '0x'; // Destination 'string'
do {++B;A& = C;
switch(A) {case 0xA: A='A'break;
case 0xB: A='B'break;
case 0xC: A='C'break;
case 0xD: A='D'break;
case 0xE: A='E'break;
case 0xF: A='F'break;
A = (A);}S += A;
D >>>= 0x04;A = D;} while(D)
doH += S[B];while (B--)
S = B = A = C = D; // Zero out variablesalert(H); // H: holds hexadecimal equivalent
const hugeNumber = "9007199254740991873839" // Make sure its in Stringconst hexOfHugeNumber = BigInt(hugeNumber).toString(16);console.log(hexOfHugeNumber)
function decimalToPaddedHexString(number, bitsize){let byteCount = Math.ceil(bitsize/8);let maxBinValue = Math.pow(2, bitsize)-1;
/* In node.js this function fails for bitsize above 32bits */if (bitsize > 32)throw "number above maximum value";
/* Conversion to unsigned form based on */if (number < 0)number = maxBinValue + number + 1;
return "0x"+(number >>> 0).toString(16).toUpperCase().padStart(byteCount*2, '0');}
测试脚本:
for (let n = 0 ; n < 64 ; n++ ) {let s=decimalToPaddedHexString(-1, n);console.log(`decimalToPaddedHexString(-1,${(n+"").padStart(2)}) = ${s.padStart(10)} = ${("0b"+parseInt(s).toString(2)).padStart(34)}`);}