Problems in using Buffered data

Hi,

I am a new Arduino UNO user, facing a problem while implementing one simple program. I am trying to decrypt something I received in my receiver. Now I used this program to receive:

void loop(){
  if(Serial.available()>0){
    char a[20]={Serial.read()};

upto this my program is working ok. But when I am trying to do something with this received data I am facing problem. Like if I even write

Serial.print("Decrypted : ");
  Serial.println(a);

This is showing something like this

 Decrypted: 1
Decrypted: 2
Decrypted: 3

where as it should show something like this Decrypted:123
Don't understand how to solve this problem, please help me!

Serial.read() returns a single byte. You will need to read them one at a time into your char array. Don't forget to make sure you have all the data before you stop reading.

I am a new Arduino UNO user, facing a problem while implementing one simple program. I am trying to decrypt something I received in my receiver.

Show the format of the data being sent would be helpful. Are delimiters being used? The below demonstrates one way the bytes being sent can be collected for various evaluation methods.

// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial test 0021"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char c = Serial.read();
    readString += c;
  }

  if (readString.length() >0) {
    Serial.println(readString);

    readString="";
  } 
}
    char a[20]={Serial.read()};

That code won’t even compile. You can not assign one character to an array of 20 characters. You can’t assign a block of code to an array, either.

Use the space bar!

PaulS:
That code won't even compile. You can not assign one character to an array of 20 characters. You can't assign a block of code to an array, either.

Just to be pedantic, yes, it does compile.
You can initialize an array like this. Usually it's used to 0-fill an array (char a[20] = {0};).
It certainly doesn't do what the OP want to do/thinks it does, but it is valid C++.

Use the space bar!

+1

Thanks to each of you for finding time answering my query. It worked the way you guys suggested. Thanks!