如何在JavaScript中将十进制值转换为十六进制值?


当前回答

function toHex(d) {
    return  ("0"+(Number(d).toString(16))).slice(-2).toUpperCase()
}

其他回答

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
}

如何在JavaScript中将十进制转换为十六进制

我没能找到一个非常干净/简单的十进制到十六进制的转换,它不涉及一堆函数和数组。。。所以我必须自己做这个。

function DecToHex(decimal) { // Data (decimal)

    length = -1;    // Base string length
    string = '';    // 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'

    do
        decimal += string[length];
    while (length--); // Flip string forwards, with the prefixed '0x'

    return (decimal); // return (hexadecimal);
}

/* Original: */

D = 3678;    // Data (decimal)
C = 0xF;    // Check
A = D;        // Accumulate
B = -1;        // Base string length
S = '';        // 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)

do
    H += S[B];
while (B--)

S = B = A = C = D; // Zero out variables
alert(H);    // H: holds hexadecimal equivalent

问题基本上是需要多少填充零。

如果您希望从数字1和17中选择字符串01和11。最好使用Buffer作为桥,用它将数字转换为字节,然后十六进制只是它的输出格式。字节组织由Buffer函数很好地控制,如writeUInt32BE、writeInt16LE等。

import { Buffer } from 'buffer';

function toHex(n) { // 4byte
  const buff = Buffer.alloc(4);
  buff.writeInt32BE(n);
  return buff.toString('hex');
}

> toHex(1)
'00000001'
> toHex(17)
'00000011'
> toHex(-1)
'ffffffff'
> toHex(-1212)
'fffffb44'
> toHex(1212)
'000004bc'

如果您希望转换为“完整”的JavaScript或CSS表示形式,可以使用以下方法:

  numToHex = function(num) {
    var r=((0xff0000&num)>>16).toString(16),
        g=((0x00ff00&num)>>8).toString(16),
        b=(0x0000ff&num).toString(16);
    if (r.length==1) { r = '0'+r; }
    if (g.length==1) { g = '0'+g; }
    if (b.length==1) { b = '0'+b; }
    return '0x'+r+g+b;                 // ('#' instead of'0x' for CSS)
  };

  var dec = 5974678;
  console.log( numToHex(dec) );        // 0x5b2a96

rgb(255255255)//返回FFFFFFrgb(255255300)//返回FFFFFFrgb(0,0,0)//返回000000rgb(148,0,211)//返回9400D3函数rgb(…值){返回值。reduce((acc,cur)=>{让val=cur>=255?'ff':cur<=0?'00':数字(cur).toString(16);返回acc+(val.length==1?“0”+val:val);},'').toUpperCase();}