How to convert String to Binary code (ASCII) ? HELP ME PLEASE I'M STUCK

Alright… i’m trying to make something for more then a day so please help me with this part.

Basically I need to convert String to Binary code ASCII.

I tried to make a function that whatever String comes into it, it returns integer array that is converted by ASCII binary code.

In the setup function I tried to make it work so it’s not too important.

My qustion is: How do I make this work?
** What is my mistakes?**

Thank you for helping.

void setup() {
  Serial.begin(9600);
  
    String sRead = "koren";
    Serial.print("sRead: " + sRead + "\n");
    int x[ sRead.length() ] = *binaryConverter(sRead);
    Serial.println("binary: " + x );
    
    for(int i=0;i<sRead.length();i++) Serial.println("binary: " + x[i] );
}

void loop() {
  
}


int binaryConverter(String input){

  int binaryCode[ input.length() ];
  
  for(int i=0;i<input.length();i++)
  {

    switch( input.charAt(i) ){

      case ' ':
      binaryCode[i] = B00100000;
      break;
      case 'a':
      binaryCode[i] = B01100001;
      break;
      case 'b':
      binaryCode[i] = B01100010;
      break;
      case 'c':
      binaryCode[i] = B01100011;
      break;
      case 'd':
      binaryCode[i] = B01100100;
      break;
      case 'e':
      binaryCode[i] = B01100101;
      break;
      case 'f':
      binaryCode[i] = B01100110;
      break;
      case 'g':
      binaryCode[i] = B01100111;
      break;
      case 'h':
      binaryCode[i] = B01101000;
      break;
      case 'i':
      binaryCode[i] = B01101001;
      break;
      case 'j':
      binaryCode[i] = B01101010;
      break;
      case 'k':
      binaryCode[i] = B01101011;
      break;
      case 'l':
      binaryCode[i] = B01101100;
      break;
      case 'm':
      binaryCode[i] = B01101101;
      break;
      case 'n':
      binaryCode[i] = B01101110;
      break;
      case 'o':
      binaryCode[i] = B01101111;
      break;
      case 'p':
      binaryCode[i] = B01110000;
      break;
      case 'q':
      binaryCode[i] = B01110001;
      break;
      case 'r':
      binaryCode[i] = B01110010;
      break;
      case 's':
      binaryCode[i] = B01110011;
      break;
      case 't':
      binaryCode[i] = B01110100;
      break;
      case 'u':
      binaryCode[i] = B01110101;
      break;
      case 'v':
      binaryCode[i] = B01110110;
      break;
      case 'w':
      binaryCode[i] = B01110111;
      break;
      case 'x':
      binaryCode[i] = B01111000;
      break;
      case 'y':
      binaryCode[i] = B01111001;
      break;
      case 'z':
      binaryCode[i] = B01111010;
      break;

      default:
      break;
    }
    Serial.print(input.charAt(i) + " == " + binaryCode[i]);
  }
  return binaryCode;
}

That’s quite a misunderstanding of quantity versus representation coding in a computer… what you do is useless… For example zero in ASCII can be printed out as 48 in base 10, 060 in base 8, 30 in base 16 and 0110000 in base 2 (or ‘0’ as a char) the memory representation is a quantity and always the same

To start Don’t use String. Use char arrays.

Then for each char use the bitRead() function to iterate over the 8 bits and print 0 or 1 (Or use print(aChar, BIN); but you won’t get nice 0 in front to have 8 digits always with that one that is 15 will be 1111 and not 00001111)

So you don’t need to reinvent the wheel, binary is just a representation. Your number is a quantity. 15 in decimal is the same quantity as F in hexadecimal or 1111 in binary or XV in roman numbers. It’s a mathematical representation of your quantity => So your character coding is already the right “quantity” you just need to display it the right way…

.

Solved my own problem

here’s what I worte:

#define led 12
#define ldr A0

String data = "my name is koren";
int BinaryData[50];


void setup() {
  Serial.begin(9600);
  pinMode(led,OUTPUT);
  digitalWrite(led,1);
  
  stringToDigital(data);
  
  startSignal();
  for(int i=0;i<data.length();i++){
    transmit(BinaryData[i]);
  }
  endSignal();
  
  
}

void loop() {
 
}



void startSignal(){
  int startOfText = B00010001;   //Device Control 1
  int binaryNum,multi=B10000000,sum=0x00;
   Serial.println("\t\tStart signal Transmitted." );

  for(int i=7;i>=0;i--){
    
    binaryNum = startOfText & multi;
    
    binaryNum = binaryNum >> i;
    multi/=2;
    
    digitalWrite(led,binaryNum);
  }
}

void endSignal(){
  int endOfText = B00010011;   //Device Control 3
  int binaryNum,multi=B10000000,sum=0x00;
   Serial.println("\t\tEnd signal Transmitted." );

  for(int i=7;i>=0;i--){
    
    binaryNum = endOfText & multi;
    
    binaryNum = binaryNum >> i;
    multi/=2;
    
    digitalWrite(led,binaryNum);
  }
}

void transmit(int dataTo){
   int binaryNum,multi=B10000000,sum=0x00;
   Serial.print("\tBinary transmitted: " );
   Serial.println( dataTo , BIN);

  for(int i=7;i>=0;i--){
    
    binaryNum = dataTo & multi;
    
    binaryNum = binaryNum >> i;
    multi/=2;
    
    digitalWrite(led,binaryNum);
  }
}


void stringToDigital(String data){
  int len;
  if(data.length() > 50)len = 50;
  else len=data.length();
  
  for(int i=0;i<len;i++){
    
    switch(data.charAt(i)){
      case ' ':
      BinaryData[i] = B00100000;
      break;
      case 'a':
      BinaryData[i] = B01100001;
      break;
      case 'b':
      BinaryData[i] = B01100010;
      break;
      case 'c':
      BinaryData[i] = B01100011;
      break;
      case 'd':
      BinaryData[i] = B01100100;
      break;
      case 'e':
      BinaryData[i] = B01100101;
      break;
      case 'f':
      BinaryData[i] = B01100110;
      break;
      case 'g':
      BinaryData[i] = B01100111;
      break;
      case 'h':
      BinaryData[i] = B01101000;
      break;
      case 'i':
      BinaryData[i] = B01101001;
      break;
      case 'j':
      BinaryData[i] = B01101010;
      break;
      case 'k':
      BinaryData[i] = B01101011;
      break;
      case 'l':
      BinaryData[i] = B01101100;
      break;
      case 'm':
      BinaryData[i] = B01101101;
      break;
      case 'n':
      BinaryData[i] = B01101110;
      break;
      case 'o':
      BinaryData[i] = B01101111;
      break;
      case 'p':
      BinaryData[i] = B01110000;
      break;
      case 'q':
      BinaryData[i] = B01110001;
      break;
      case 'r':
      BinaryData[i] = B01110010;
      break;
      case 's':
      BinaryData[i] = B01110011;
      break;
      case 't':
      BinaryData[i] = B01110100;
      break;
      case 'u':
      BinaryData[i] = B01110101;
      break;
      case 'v':
      BinaryData[i] = B01110110;
      break;
      case 'w':
      BinaryData[i] = B01110111;
      break;
      case 'x':
      BinaryData[i] = B01111000;
      break;
      case 'y':
      BinaryData[i] = B01111001;
      break;
      case 'z':
      BinaryData[i] = B01111010;
      break;

      default:
      break;
    }
      
  }
}

[code]/

why do you ask question if you don’t listen… that’s a really poor solution you have… here is an example of what I meant, doing the same thing… See the difference?

const byte led = 13;

const byte startOfText = B00010001;   //Device Control 1
const byte endOfText = B00010011;     //Device Control 3

char data[] = "my name is koren";

void bitSignal(byte b)
{
  Serial.print((char) b);
  Serial.print("\tBinary transmitted: ");
  for (int i = 7; i >= 0; --i) {
    Serial.print(bitRead(b, i));
    digitalWrite(led, bitRead(b, i)); 
    // delay(50); // uncomment to see the blinking
  }
  Serial.println();
}

void setup() {
  int nbBytes = strlen(data);
  
  Serial.begin(9600); // you could use 115200
  while (!Serial);
  pinMode(led, OUTPUT);
  digitalWrite(led, HIGH);
  Serial.println("\n------------------------------------");
  Serial.print("Start" );  bitSignal(startOfText);
  for (int i = 0; i < nbBytes; i++) bitSignal((byte) data[i]);
  Serial.print("End" );  bitSignal(endOfText);
}

void loop() {}

when you do

      case 'a':
      BinaryData[i] = B01100001;

you could as well do BinaryData[i] = 'a';it’s really the same thing… so your whole danse with the huge case and the manual work is totally useless.

Thank you so much. I learned a lot from you.