ASCII " Binary to text"

Hallo everyone, thanks for you guys' help for my previous questions.
My project is coming to an end with this final question.

Can I ask for the help about how to transfer Binary to Text? Example: 10000000 to A.

For my program, binary signals are received from Tx[Array] to Rx side.

So now I can accurately collect the binary codes and now I'm trying to change the binary codes to characters. (8bit to one text. 8bit to one text.... so on)

I have read about the following code from the tutorial of Arduino. However, it's teaching how to add dec. into binary/hex.

Can anyone give me some clear advice of how to change those binary codes to text?

Best regards,
Timothy

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // prints title with ending line break
  Serial.println("ASCII Table ~ Character Map");
}

// first visible ASCIIcharacter '!' is number 33:
int thisByte = 33;
// you can also write ASCII characters in single quotes.
// for example. '!' is the same as 33, so you could also use this:
//int thisByte = '!';

void loop() {
  // prints value unaltered, i.e. the raw binary version of the
  // byte. The serial monitor interprets all bytes as
  // ASCII, so 33, the first number,  will show up as '!'
  Serial.write(thisByte);

  Serial.print(", dec: ");
  // prints value as string as an ASCII-encoded decimal (base 10).
  // Decimal is the  default format for Serial.print() and Serial.println(),
  // so no modifier is needed:
  Serial.print(thisByte);
  // But you can declare the modifier for decimal if you want to.
  //this also works if you uncomment it:

  // Serial.print(thisByte, DEC);


  Serial.print(", hex: ");
  // prints value as string in hexadecimal (base 16):
  Serial.print(thisByte, HEX);

  Serial.print(", oct: ");
  // prints value as string in octal (base 8);
  Serial.print(thisByte, OCT);

  Serial.print(", bin: ");
  // prints value as string in binary (base 2)
  // also prints ending line break:
  Serial.println(thisByte, BIN);

  // if printed last visible character '~' or 126, stop:
  if (thisByte == 126) {    // you could also use if (thisByte == '~') {
    // This loop loops forever and does nothing
    while (true) {
      continue;
    }
  }
  // go on to the next character
  thisByte++;
}
[b]"Output of the previous codes"[/b]

ASCII Table ~ Character Map
!, dec: 33, hex: 21, oct: 41, bin: 100001
", dec: 34, hex: 22, oct: 42, bin: 100010
#, dec: 35, hex: 23, oct: 43, bin: 100011
$, dec: 36, hex: 24, oct: 44, bin: 100100
%, dec: 37, hex: 25, oct: 45, bin: 100101
&, dec: 38, hex: 26, oct: 46, bin: 100110
', dec: 39, hex: 27, oct: 47, bin: 100111
(, dec: 40, hex: 28, oct: 50, bin: 101000
), dec: 41, hex: 29, oct: 51, bin: 101001
*, dec: 42, hex: 2A, oct: 52, bin: 101010
+, dec: 43, hex: 2B, oct: 53, bin: 101011
,, dec: 44, hex: 2C, oct: 54, bin: 101100
-, dec: 45, hex: 2D, oct: 55, bin: 101101
., dec: 46, hex: 2E, oct: 56, bin: 101110
/, dec: 47, hex: 2F, oct: 57, bin: 101111
0, dec: 48, hex: 30, oct: 60, bin: 110000
1, dec: 49, hex: 31, oct: 61, bin: 110001
2, dec: 50, hex: 32, oct: 62, bin: 110010
3, dec: 51, hex: 33, oct: 63, bin: 110011
4, dec: 52, hex: 34, oct: 64, bin: 110100
5, dec: 53, hex: 35, oct: 65, bin: 110101
6, dec: 54, hex: 36, oct: 66, bin: 110110
7, dec: 55, hex: 37, oct: 67, bin: 110111
8, dec: 56, hex: 38, oct: 70, bin: 111000
9, dec: 57, hex: 39, oct: 71, bin: 111001
:, dec: 58, hex: 3A, oct: 72, bin: 111010
;, dec: 59, hex: 3B, oct: 73, bin: 111011
<, dec: 60, hex: 3C, oct: 74, bin: 111100
=, dec: 61, hex: 3D, oct: 75, bin: 111101

, dec: 62, hex: 3E, oct: 76, bin: 111110
?, dec: 63, hex: 3F, oct: 77, bin: 111111
@, dec: 64, hex: 40, oct: 100, bin: 1000000
A, dec: 65, hex: 41, oct: 101, bin: 1000001
B, dec: 66, hex: 42, oct: 102, bin: 1000010
C, dec: 67, hex: 43, oct: 103, bin: 1000011
D, dec: 68, hex: 44, oct: 104, bin: 1000100
E, dec: 69, hex: 45, oct: 105, bin: 1000101

Maybe Serial.write(thisByte) ?

timothycat:
For my program, binary signals are received from Tx[Array] to Rx side.

So now I can accurately collect the binary codes and now I'm trying to change the binary codes to characters. (8bit to one text. 8bit to one text.... so on)

You need to explain that more clearly and post the program that is receiving the data.

Can you post some examples of the data that is being received?

Internally an Arduino (like every computer) stores everything as binary. So, for example, the character 'A' is stored as 01000001 which has the decimal value 65 and the HEX value 41. If you have a byte variable myVal containing that value you can make it show as the character 'A' using Serial.print( (char) myVal); or you can make it show as the character "65" by using Serial.print(myVal);

...R

Here's my output from the receiving side.

The binary codes received are serial.print on the COM monitor. I want to change the binary codes into words..... is it possible?

Thank you sir... I'm a beginner of Arduino learner.

timothycat:

Here's my output from the receiving side.

Without seeing the code that produced that output, your question can not be answered.

Here's my code sir.
The previous 1010101110 Output is collected from the two serial.print 1 and 0. so it appears as a long string.

Can I ask about how to change them into words?

 int pd=2;                      //pin 2 Connect to photodiode
 int ledshow=13;                 //pin 13 Connect to Ledshow 
 int senRead=0;                 //Readings from sensor to analog pin A0  
 int limit=900;                 //Threshold range of accepting light  
 int n=0;
 void setup()    
 {  
  pinMode(pd,OUTPUT);  
  pinMode(ledshow,OUTPUT);  
  digitalWrite(pd,HIGH);       //supply 5 volts to photodiode  
  digitalWrite(ledshow,LOW);   //set the ledshow in off mode (initial condition)  
  Serial.begin(9600);          //setting serial monitor at a default baund rate of 9600  
 }  
 void loop()  
 {  
  int val=analogRead(senRead);  //variable to store values from the photodiode  

  if(val <= limit)             //"HIGH" signal received 
  {  
   Serial.print(1);          // prints the values from the ledshow in serial monitor  
   digitalWrite(ledshow,HIGH);     // ledshow will be in ON state  
    while(n==0)
  {n++;
   if(n==100)
   {n=0;
    break;}
  }
  }
  else if(val > limit)            ("LOW" Signal Received)
  {
   Serial.print(0);          // prints the values from the ledshow in serial monitor  
   digitalWrite(ledshow,LOW);      // ledshow will be in OFF state  
    while(n==0)
  {n++;
   if(n==100)
   {n=0;
    break;}
  }
 } 
 }

Don't print the bits, put 8 of them into a byte and add it to a buffer.

You just put out the state of the receiver around 1000 times a second, slowed down by the serial output.
Your 'timing loops' will probably be removed by the optimizer.

Without a proper timing and some start-bit-detection you can not make sense out of the incoming data.

Whandall:
Don't print the bits, put 8 of them into a byte and add it to a buffer.

You just put out the state of the receiver around 1000 times a second, slowed down by the serial output.
Your 'timing loops' will probably be removed by the optimizer.

Without a proper timing and some start-bit-detection you can not make sense out of the incoming data.

I'll try it sir.

The delay function causes some error. So I replace it with the while loop counting function. My aim is receiving around 1000 times per second >.< ! I think the while loop "timing delay" function is Okay.

The Start-bit-detection I will now try to make it sir.

You mean 8bits - 8bits -8bits. like that? Should I use an Array function to group them up?

timothycat:
So I replace it with the while loop counting function.
I think the while loop "timing delay" function is Okay.

Not in the current version.

timothycat:
My aim is receiving around 1000 times per second >.< !

Use the 'Blink without delay' principle with micros() and not millis().

timothycat:
The Start-bit-detection I will now try to make it sir.

Are there additional start and stop bits in your protocol, like in normal serial communication?
Then you would need to remove them (by ignoring / not storing).

timothycat:
You mean 8bits - 8bits -8bits. like that? Should I use an Array function to group them up?

A byte/char array could be useful.

timothycat:
Here's my code sir.

What is producing the signal that your photo-diode is detecting?

How can you expect useful advice when you don't tell us all about the project?

...R

I think the while loop "timing delay" function is Okay.

It definitely is not.

   while (n == 0) {
      n++;
      if (n == 100) {
        n = 0;
        break;
      }
    }

If n is not zero, the loop does not execute at all. If n is zero, then it is incremented to be 1 which causes the loop to exit. So either the loop is bypassed altogether or it executes once. Either way, the compiler will probably detect that the loop isn't doing anything worthwhile and will optimize it into oblivion.

Pete

1 Like

el_supremo:
It definitely is not.

All discussion is academic until we are told what is producing the signals that the the photo-diode is detecting.

For all we know it is a regular serial signal that could be interpreted with SoftwareSerial.

...R

Problem Solved.

Thank you!!!

      while (n == 0){
        n++;
        if (n == 100){
          n = 0;
          break;
        }
      }

How many times will that while statement iterate? If n is 0 when the statement is encountered, it will be incremented to 1, which is not 100, so the while statement ends, and may repeat. n is now not 0, so it ends., having accomplished no more than

  if(n == 0)
    n = 1;

If n is not 0 when the while statement is encountered, the body is skipped.

Why on earth are you setting n to 1 if it was 0 in such a complicated fashion? Are you being paid by the line of code?

    else if (val > limit)           // ("LOW" Signal Received)

If val was not less than or equal to limit, is there ANY possibility that this if statement will NOT be true?

I do not understand what you are trying to do.

Why have you started a new Thread about the exact same problem - and failed to answer the question in your other Thread.

I am suggesting to the Moderator to merge them so we have all the info in the same place.

...R

@timothycat, do not cross-post. Threads merged.