Invert GMLscripts.com

bin_to_dec

Converts a string of binary digits to a decimal value.

dec = bin_to_dec("00100");      //  dec == 4
dec = bin_to_dec("10101");      //  dec == 21
dec = bin_to_dec("11111");      //  dec == 31
bin_to_dec(bin)
Returns an integer converted from a binary string.
COPY/// @func   bin_to_dec(bin)
///
/// @desc   Returns an integer converted from a binary string.
///
/// @param  {string}    bin         binary digits
///
/// @return {real}      positive integer
///
/// GMLscripts.com/license

function bin_to_dec(bin) 
{
    var dec = 0;

    var len = string_length(bin);
    for (var pos = 1; pos <= len; pos += 1) {
        dec = dec << 1;
        if (string_char_at(bin, pos) == "1") dec |= 1;
    }

    return dec;
}

Contributors: xot

GitHub: View · Commits · Blame · Raw