# dec_to_hex

Converts a decimal value to a string of hexadecimal digits.

hex = dec_to_hex(123);          //  hex == "7B"
hex = dec_to_hex(456);          //  hex == "1C8"
hex = dec_to_hex(789, 4);       //  hex == "0315"
hex = dec_to_hex(-43, 4);       //  hex == "FFD5"
dec_to_hex(dec, len)
Returns a given value as a string of hexadecimal digits.
COPY/// @func   dec_to_hex(dec, len)
///
/// @desc   Returns a given value as a string of hexadecimal digits.
///         Hexadecimal strings can be padded to a minimum length.
///         Note: If the given value is negative, it will
///         be converted using its two's complement form.
///
/// @param  {real}      dec         integer
/// @param  {real}      [len=1]     minimum number of digits
///
/// @return {string}    hexadecimal digits
///
/// GMLscripts.com/license

function dec_to_hex(dec, len = 1)
{
var hex = "";

if (dec < 0) {
len = max(len, ceil(logn(16, 2 * abs(dec))));
}

var dig = "0123456789ABCDEF";
while (len-- || dec) {
hex = string_char_at(dig, (dec & \$F) + 1) + hex;
dec = dec >> 4;
}

return hex;
}


Contributors: xot

GitHub: View · Commits · Blame · Raw