phpInDev

text_to_binary

string text_to_binary ( string text )

This converts given text into 8 bit binary coding (meaning that each character is represented by 8 bits). The normal ascii representation of the character is used (so that each character has it's ascii value in the binary text).

Example 1. Using text_to_binary

{ show_message(text_to_binary("It works!")); }

Shows message:

010010010111010000100000011101110110111101110010011010110111001100100001

Code for text_to_binary.gml

{
    var len, i, ascii, retval, binary;
    
    len = string_length(argument0);
    retval = "";
    
    // Convert each character
    for (i = 1; i <= len; i += 1)
    {
        ascii = ord(string_char_at(argument0, i));
        binary = "";
        
        // Get each bit of the character
        repeat (8)
        {
            // binary = string(ascii & 1) + binary; (with 10x speed)
            if (ascii & 1)
            {
                binary = "1" + binary;
            }
            else
            {
                binary = "0" + binary;
            }
            ascii = ascii >> 1;
        }
        
        retval += binary;
    }
    
    return retval;
}