Invert GMLscripts.com

dec_to_oct

Converts a decimal value to a string of octal digits.

oct = dec_to_oct(123);          //  oct == "173"
oct = dec_to_oct(456);          //  oct == "710"
oct = dec_to_oct(789, 6);       //  oct == "001425"
oct = dec_to_oct(-43, 6);       //  oct == "777725"
dec_to_oct(dec [, len=1])
Returns a given value as a string of octal digits.
COPY/// @func   dec_to_oct(dec [, len=1])
///
/// @desc   Returns a given value as a string of octal digits.
///         Octal 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}    octal digits
///
/// GMLscripts.com/license

function dec_to_oct(dec, len)
{
    len = is_undefined(len) ? 1 : len;
    var oct = "";

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

    var dig = "01234567";
    while (len-- || dec) {
        oct = string_char_at(dig, (dec & $7) + 1) + oct;
        dec = dec >> 3;
    }

    return oct;
}

Contributors: xot

GitHub: View · Commits · Blame · Raw