Hướng dẫn number to hex javascript

The accepted answer did not take into account single digit returned hexadecimal codes. This is easily adjusted by:

function numHex[s]
{
    var a = s.toString[16];
    if [[a.length % 2] > 0] {
        a = "0" + a;
    }
    return a;
}

and

function strHex[s]
{
    var a = "";
    for [var i=0; i4],1] + l.substr[[c & 0x0f],1];
    }

    return "0x" + o;
}

This is a very fast function that takes into account single digits, floating point numbers, and even checks to see if the person is sending a hex value over to be hexed again. It only uses four function calls and only two of those are in the loop. To un-hex the values you use:

/////////////////////////////////////////////////////////////////////////////
//  fromHex[].  Convert a hex string to ASCII text.
/////////////////////////////////////////////////////////////////////////////
fromHex[s]
{
    var start = 0;
    var o = "";

    if [s.substr[0,2].toLowerCase[] == "0x"] {
        start = 2;
    }

    if [typeof s != "string"] {
        s = s.toString[];
    }
    for [var i=start; i

Chủ Đề