如何在JavaScript中将十进制值转换为等效的十六进制值?
如何在JavaScript中将十进制转换为十六进制
You can do something like this in ECMAScript 6:
const toHex = num => (num).toString(16).toUpperCase();
As the accepted answer states, the easiest way to convert from decimal to hexadecimal is var hex = dec.toString(16)
. However, you may prefer to add a string conversion, as it ensures that string representations like "12".toString(16)
work correctly.
// Avoids a hard-to-track-down bug by returning `c` instead of `12`
(+"12").toString(16);
To reverse the process you may also use the solution below, as it is even shorter.
var dec = +("0x" + hex);
It seems to be slower in Google Chrome and Firefox, but is significantly faster in Opera. See http://jsperf.com/hex-to-dec.
AFAIK comment 57807 is wrong and should be something like: var hex = Number(d).toString(16); instead of var hex = parseInt(d, 16);
function decimalToHex(d, padding) {
var hex = Number(d).toString(16);
padding = typeof (padding) === "undefined" || padding === null ? padding = 2 : padding;
while (hex.length < padding) {
hex = "0" + hex;
}
return hex;
}
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
}
Constrained/padded to a set number of characters:
function decimalToHex(decimal, chars) {
return (decimal + Math.pow(16, chars)).toString(16).slice(-chars).toUpperCase();
}
var number = 3200;
var hexString = number.toString(16);
The 16 is the radix and there are 16 values in a hexadecimal number :-)
Combining some of these good ideas for an RGB-value-to-hexadecimal function (add the #
elsewhere for HTML/CSS):
function rgb2hex(r,g,b) {
if (g !== undefined)
return Number(0x1000000 + r*0x10000 + g*0x100 + b).toString(16).substring(1);
else
return Number(0x1000000 + r[0]*0x10000 + r[1]*0x100 + r[2]).toString(16).substring(1);
}
function toHex(d) {
return ("0"+(Number(d).toString(16))).slice(-2).toUpperCase()
}
With padding:
function dec2hex(i) {
return (i+0x10000).toString(16).substr(-4).toUpperCase();
}
为了完整起见,如果要用负数的二进制补码十六进制表示形式,则可以使用zero-fill-right shift >>>
运算符。例如:
> (-1).toString(16)
"-1"
> ((-2)>>>0).toString(16)
"fffffffe"
但是,存在一个局限性:JavaScript按位运算符将其操作数视为32位序列,也就是说,您获得了32位二进制补码。
如果您需要处理位字段或32位颜色之类的内容,则需要处理带符号的数字。JavaScript函数toString(16)
将返回负十六进制数,通常这不是您想要的。此功能做了一些疯狂的添加,使其成为正数。
function decimalToHexString(number)
{
if (number < 0)
{
number = 0xFFFFFFFF + number + 1;
}
return number.toString(16).toUpperCase();
}
console.log(decimalToHexString(27));
console.log(decimalToHexString(48.6));
下面的代码会将十进制值d转换为十六进制。它还允许您将填充添加到十六进制结果中。因此,默认情况下0将变为00。
function decimalToHex(d, padding) {
var hex = Number(d).toString(16);
padding = typeof (padding) === "undefined" || padding === null ? padding = 2 : padding;
while (hex.length < padding) {
hex = "0" + hex;
}
return hex;
}
使用以下命令将数字转换为十六进制字符串:
hexString = yourNumber.toString(16);
并通过以下步骤逆转该过程:
yourNumber = parseInt(hexString, 16);
If you are looking for converting Large integers i.e. Numbers greater than Number.MAX_SAFE_INTEGER -- 9007199254740991, then you can use the following code