0

I found a script that converts binary to string but how can I input a string and get the binary representation? so say I put in "P" I want it to output 01010000 as a string.

I have this but it is not what I am trying to do - it converts a string containing a binary number into a real value of that number:

///string_to_binary(string)
var str = argument0;
var output = "";
for(var i = 0; i < string_length(str); i++){
if(string_char_at(str, i + 1) == "0"){
    output += "0";
}
else{
    output += "1";
}
}
return real(output);
Jerry Jeremiah
  • 9,045
  • 2
  • 23
  • 32
BigDuke6
  • 117
  • 1
  • 13
  • Update your question to include actual language you are using please, cant really help you otherwise, and not easily searchable for others in future. http://stackoverflow.com/help/how-to-ask – Daniel Brose Mar 24 '17 at 00:01
  • Instead of string_char_at() the function you want is string_ord_at() - that gets the numeric value of the character at that spot. Then you need to convert that numeric value into binary - the java example from the answer below is probably the most helpful of the three since the others rely on built in data types and functions that game maker doesn't have. – Jerry Jeremiah Mar 24 '17 at 00:09
  • @DanielBrose Game Maker language __IS__ a language https://docs.yoyogames.com/source/dadiospice/002_reference/ – Jerry Jeremiah Mar 24 '17 at 00:10
  • @JerryJeremiah - thanks for that, i didnt realise as i searched and didnt see it, and the tag added is an IDE tag – Daniel Brose Mar 24 '17 at 00:12

3 Answers3

2

Tip: search for GML or other language term, these questions answered many times. Also please check your tag as it is the IDE tag, not language tag.

Im not familiar with GML myself, but a quick search showed this:

At least semi-official method for exactly this: http://www.gmlscripts.com/script/bytes_to_bin

/// bytes_to_bin(str)
//
//  Returns a string of binary digits, 1 bit each.
//
//      str         raw bytes, 8 bits each, string
//
/// GMLscripts.com/license
{
    var str, bin, p, byte;
    str = argument0;
    bin = "";
    p = string_length(str);
    repeat (p) {
        byte = ord(string_char_at(str,p));
        repeat (8) {
            if (byte & 1) bin = "1" + bin else bin = "0" + bin;
            byte = byte >> 1;
        }
        p -= 1;
    }
    return bin;
}

GML forum (has several examples) https://www.reddit.com/r/gamemaker/comments/4opzhu/how_could_i_convert_a_string_to_binary/

///string_to_binary(string)
var str = argument0;
var output = "";
for(var i = 0; i < string_length(str); i++){
    if(string_char_at(str, i + 1) == "0"){
        output += "0";
    }
    else{
        output += "1";
    }
}
return real(output);


And other language examples:

C++ Fastest way to Convert String to Binary?

#include <string>
#include <bitset>
#include <iostream>
using namespace std;
int main(){
  string myString = "Hello World";
  for (std::size_t i = 0; i < myString.size(); ++i)
  {
      cout << bitset<8>(myString.c_str()[i]) << endl;
  }
}

Java: Convert A String (like testing123) To Binary In Java

  String s = "foo";
  byte[] bytes = s.getBytes();
  StringBuilder binary = new StringBuilder();
  for (byte b : bytes)
  {
     int val = b;
     for (int i = 0; i < 8; i++)
     {
        binary.append((val & 128) == 0 ? 0 : 1);
        val <<= 1;
     }
     binary.append(' ');
  }
  System.out.println("'" + s + "' to binary: " + binary);

JS: How to convert text to binary code in JavaScript?

function convert() {
  var output = document.getElementById("ti2");
  var input = document.getElementById("ti1").value;
  output.value = "";
  for (var i = 0; i < input.length; i++) {
      output.value += input[i].charCodeAt(0).toString(2) + " ";
  }
}
Community
  • 1
  • 1
Daniel Brose
  • 1,394
  • 10
  • 24
  • How would I go about converting the binary encoded string BACK to it's original string? Thanks :) – Momoro Dec 17 '20 at 06:09
  • @Momoro - see those included links and similar searches for whatever language your working with, as said above Im not familiar with GML, just found many examples of the answer in first quick search i did, and just shared some well written answers – Daniel Brose Feb 09 '21 at 05:26
0

I was looking around for a simple GML script to convert a decimal to binary and return the bits in an array. I didn't find anything for my need and to my liking so I rolled my own. Short and sweet.

The first param is the decimal number (string or decimal) and the second param is the bit length.

// dec_to_bin(num, len);
// argument0, decimal string
// argument1, integer

var num = real(argument0);
var len = argument1;
var bin = array_create(len, 0);

for (var i = len - 1; i >= 0; --i) {
    bin[i] = floor(num % 2);
    num -= num / 2;
}

return bin;

Usage:

dec_to_bin("48", 10);

Output:

{ { 0,0,0,0,1,1,0,0,0,0 },  }
0

i think the binary you mean is the one that computers use, if thats the case, just use the common binary and add a kind of identification.

binary is actually simple, instead of what most people think. every digit represents the previous number *2 (2¹, 2², 2³...) so we get:

1, 2, 4, 8, 16, 32, 64, 128, 256, 512...

flip it and get:

...512, 256, 128, 64, 32, 16, 8, 4, 2, 1

every digit is "activated" with 1's, plus all the activated number ant thats the value.

ok, so binary is basically another number system, its not like codes or something. Then how are letters and other characters calculated?

they arent ;-;

we just represent then as their order on their alphabets, so:

a=1
b=2
c=3
...

this means that "b" in binary would be "10", but "2" is also "10". So thats where computer's binary enter.

they just add a identification before the actual number, so:

letter_10 = b
number_10 = 2
signal_10 = "

wait, but if thats binary there cant be letter on it, instead another 0's and 1's are used, so:

011_10 = b
0011_10 = 2
001_10 = "

computers also cant know where the number starts and ends, so you have to always use the same amount of numbers, which is 8. now we get:

011_00010 = b
0011_0010 = 2
001_00010 = "

then remove the "_" cuz again, computers will only use 0's and 1's. and done!

so what i mean is, just use the code you had and add 00110000 to the value, or if you want to translate these numbers to letters as i wanted just add 01100000

in that case where you have the letter and wants the binary, first convert the letter to its number, for it just knows that the letters dont start at 1, capitalized letters starts at 64 and the the non-capitalized at 96.

ord("p")=112
112-96=16
16 in binary is 10000
10000 + 01100000 = 01110000
"p" in binary is 01110000

ord("P")=80
80-64=16
16 in binary is 10000
10000 + 01000000 = 01010000
"P" in binary is 01010000

thats just a explanation of what the code should do, actually im looking for a simple way to turn binary cuz i cant understand much of the code you showed.

(011) 1000 1111 10000 101 1001 1000 101 1100 10000 101 100

Pamoy
  • 1
  • 2