Assigning letters to binary code by switch case

I have a char array of the length of an sentence named inputText[_]

char inputText[] = {"Hello World"};
const size_t sizeText = strlen(inputText);
int outputText[sizeText]; 

I would then like to assign each individual letter its own binary code

for (int i = 0; i < sizeText; i++) {  
  switch (inputText[i]) {
    case 'H': 
    outputText[i] = {00001};  
    break;
    case 'e': 
    outputText[i] = {00010};  
    break;
    case 'l': 
    outputText[i] = {00011};  
    break;
    case 'o': 
    outputText[i] = {00100};  
    break;
    **case 0x20:** 
    outputText[i] = {00101};  
    break;
    case 'W': 
    outputText[i] = {00110};  
    break;
    case 'r': 
    outputText[i] = {00111};  
    break;
    case 'd': 
    outputText[i] = {01000};  
    break;
  }
}

The result should be a int array looking like:
outputText[_] = {“00000, 00001, 00010, 00011, 00011, 00100, 00101, 00110, 00100, 00111, 00011, 01000”};

May anyone know what I am doing wrong? And is it right how I check for the space case between the words?

If I understand correctly, try it this way

#for (int i = 0; i < sizeText; i++) {  
  switch (inputText[i]) {
    case 'H': 
    outputText[i] = 0x00001;  
    break;
    case 'e': 
    outputText[i] = 0x00010;  
    break;
    case 'l': 
    outputText[i] = 0x00011;  
    break;
    case 'o': 
    outputText[i] = 0x00100;  
    break;
    **case 0x20:** 
    outputText[i] = 0x00101;  
    break;
    case 'W': 
    outputText[i] = 0x00110;  
    break;
    case 'r': 
    outputText[i] = 0x00111;  
    break;
    case 'd': 
    outputText[i] = 0x01000;  
    break;
  }
}

RV mineirin

For one thing, you declare outputText as a single dimensional array, and then index it as a single dimensional array. C strings are arrays, an array of C strings is then an array of arrays, or a two dimensional array.

Check with the class notes or text book for this assignment for more clues on two dimensional arrays, it seems to be the whole point of this learning exercise.

Although, taking your output example literally, you would have to assemble the output character by character in the output buffer, rather than using two dimensions for the array. For that, you would need to maintain an index or pointer to the array and create routines to populate it. I think the professor has just given you enough rope to hang yourself. :slight_smile:

It’s likely that you misunderstood the assignment requirement.

1 Like

Your suggestion won’t produce the output the OP needs.

1 Like

If you want to use a const value represented as binary, use 0b0001 etc, if it is hex, use 0x00A etc.
Can you give more detail about your exact needs?

1 Like

@JOHI
This is a project for the transmission of information with light. The int outputText array is used as a pulse generator for an LED lamp. For example, if the letter “W” is sent. The light is switched off for 2 time units, switched on for 2 and switched off again for one as the binary code shows (00110).

That means the int outputText array is to be read out in a for loop afterwards and controls a LED light. Can you imagine the project better now?

Yes…

void setup() 
{
  Serial.begin(9600);
  delay(1000);

  char inputText[] = {"Hello World"};
  const size_t sizeText = strlen(inputText);

  String outputText;
  for (int i = 0; i < sizeText; i++) 
  {  
    switch (inputText[i]) 
    {
      case 'H': 
        outputText+= "00001 ";  
      break;
      case 'e': 
        outputText+= "00010 ";  
      break;
      case 'l': 
        outputText+= "00011 ";  
      break;
      case 'o': 
        outputText+= "00100 ";  
      break;
      case ' ': 
        outputText+= "00101 ";  
      break;
      case 'W': 
        outputText+= "00110 ";  
      break;
      case 'r': 
        outputText+= "00111 ";  
      break;
      case 'd': 
        outputText+= "01000 ";  
      break;
    }
  }
  Serial.println((String) "the complete string is" + outputText);
  for (int i=0; i < outputText.length();i++)
  {
     if (outputText[i]=='1') 
       Serial.println("ON"); 
     if (outputText[i]=='0')
       Serial.println("OFF");
  }
}

void loop() 
{

}
1 Like

@JOHI Thank you for your quick help!! The solution from you works. Thanks a lot!

any reason you can you can’t use the 7-bit ASCII value?

@gcjr I use a cmos camera as receiver. The size of the sensor decides how many bits i can get in a picture. Unfortunately, I can’t manage more than 5 bits at the moment.

considering that you must be limited to alpha character, you could substract 0x40 from the ASCII value. this avoid you creating you’re own mapping

1 Like

Great Idea. I might consider that for the future.