You are currently viewing the GMLscripts.com static mirror. Forum access and script submissions are not available through this mirror.

Invert GMLscripts.com

bin_to_bytes

This function can turn a string of binary digits into a string of raw bytes.

For instance, "01001000" is equal to 72 in decimal, which is the ASCII/Unicode value for "H".

// Convert a string of binary digits to 
// a string raw bytes spelling "HELLO"
binary = "0100100001000101010011000100110001001111";
bytes = bin_to_bytes(binary);
show_message(bytes);

NOTE: Care must be taken when using this function with GM:Studio. Unlike earlier editions of GameMaker, strings are "null-terminated" and string functions are not "binary safe". Inserting a byte with a value of zero into a string will prevent the rest of the string from being usable. Further, certain combinations of bytes may be interpreted as UTF-8 multibyte characters and results may vary on different target platforms. Storing arbitrary binary data in GM:Studio strings is NOT recommended.

bin_to_bytes(bin)
Returns a string of raw bytes (8 bits each) representing the given binary string.
COPY/// bin_to_bytes(bin)
//
//  Returns a string of raw bytes (8 bits each)
//  representing the given binary string.
//
//      bin         binary digits, string
//
/// GMLscripts.com/license
{
    var bin, str, p, byte;
    bin = argument0;
    str = "";
    p = string_length(bin);
    repeat (ceil(p / 8)) {
        byte = 0;
        repeat (8) {
            byte = byte >> 1;
            if (p) if (string_char_at(bin, p) == "1") byte = byte | 128;
            p -= 1;
        }
        str = chr(byte) + str;
    }
    return str;
}

Contributors: xot

GitHub: View · Commits · Blame · Raw