Converting Array into a string

Hello,

I’m looking for a method to convert an array into a string.

Example:

char RFID_Tag[7];
...
for (int i = 0 ; i < 7  ; i++){
    Serial.print((byte)RFID_Tag[i],HEX);
    Serial.print(" ");}

Serial Monitor Output is: DF D3 D 2F 0 0 0

Now I want to send a single string over the serial communication like this:

Serial.print(RFID_Tag)

I want the output to be also : DFD3D2F000 - without space

What I get in the Serial Monitor is: ßÓ/

My Question is, how can I convert the array into a single string with the content from the array ?

Greetings

If you don't want a space between each of the hex values then DON'T PUT ONE THERE!.

Mark

sorry, but I don’t get it ?

It’s not about the space.

It is about the string.

When I use: Serial.print(RFID_Tag)

The output is : ßÓ/

Instead of: DF D3 D 2F 0 0 0
When I use:

char RFID_Tag[7];
...
for (int i = 0 ; i < 7  ; i++){
    Serial.print((byte)RFID_Tag[i],HEX);
    Serial.print(" ");}

But I want the output of Serial.print(RFID_Tag) to be the same

My Question is, how can I convert the array into a single string with the content from the array ?

Is this an array of chars or some other data type ? If it is an array of chars then put '\0' into the final + 1 array position, making sure that the array is big enough, and you have created a string.

Hi,

perhaps I’m not explaining right want I want.

I have a char array:

char RFID_Tag[7];

I’m filling this array with data and print it out like this:

for (int i = 0 ; i < 7  ; i++){
    Serial.print((byte)RFID_Tag[i],HEX);
    Serial.print(" ");}

My Serial Monitor Output is like this: DF D3 D 2F 0 0 0

Now, what I want to do is, to print a string with the same Serial Monitor Output !
But when I simply print the RFID_Tag out like this:

Serial.print(RFID_Tag);

My Serial Monitor gives me this: ßÓ/

does anyone understands this ? :smiley:

Yes, we understand that "RFID_Tag" is not a string, and we should not expect to be able to print it as though it were a string.

Do you understand that?

I'm filling this array with data

Where? How?

    Serial.print((byte)RFID_Tag[i],HEX);

This converts the value to a string, in a specific format.

YOU CAN NOT DO THAT FOR ALL VALUES IN AN ARRAY IN ONE STEP!
Do you understand that?

It makes no difference whatsoever if there are 7 Serial.print() calls or 1. The EXACT same amount of data goes out the serial port in exactly the same amount of time. Get over it.

thank you :smiley: :smiley:

Can you please post an example program that illustrates putting the char values into the array and printing them one by one ? It does not have to be the real program, but it should illustrate what you are doing.

Sure :

byte karte[7];

void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("Serial Monitor ready...");
  
  for (int i = 0 ; i <= 6  ; i++) //Feld initialisieren
  karte[i] = 0;
}

void loop() {
  int i = 0;
  int k = 0;
  
  
  while (Serial1.available() > 0 ) {
    delay(20);
    karte[i] = Serial1.read();
    i++;
    k = 1;
    }
    
   while (k == 1){ 
    for (i ; i < 8; i++)
    karte[i] = 0;
    
    for (int i = 0 ; i < 7  ; i++){
    Serial.print(karte[i],HEX);
    Serial.print(" ");}
    
    for (int i = 0 ; i < 7  ; i++){ //Feld resetten
    karte[i] = 0;
    k = 0;}
    Serial.println();
  }   
  }
byte karte[7];
...
...
for (i ; i < 8; i++)
    karte[i] = 0;

Oops.

If all you are trying to do is print the rfid on the serial monitor than a program like this is all you need

void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
 Serial1.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly: 
  if (Serial1.available()>0){
    Serial.print(Serial1.read(),HEX);
    
  }
}

There are a number of other points about the code you provided.

First

  for (int i = 0 ; i <= 6  ; i++) //Feld initialisieren
  karte[i] = 0;

is not required. Most globals are initialized to 0 by default.

  while (Serial1.available() > 0 ) {
    delay(20);
    karte[i] = Serial1.read();
    i++;
    k = 1;
    }

You are assuming that all the data will be available at this point its not safe to do this. You should be looking for some kind of end of rfid tag marker. What does your read output at the end of the rfid data?

You must learn to auto format your code it makes it a lot easer to find bugs!

Mark

Hi Mark,

thanks for the reply.

I didn't knew, that I do not have to initializie the array before using it. But thanks for the comment :slight_smile:

The antenna that I'm using is not sending out any kind of "end bit/byte" or something.
I only got the information that it's sending out 5 Bytes in a row.

I tested every RFID-Tag I have got and the Code works fine with the delay.

To come back to my initial question:

I have read out all my rfid tags and saved them like this in my arduino as DEC numbers:

int RFID_Tag_Card1 = 2232211347000 
and so on....

When I read the Tags out, as you can see in my code, I save them in a char array like this:

char RIFD_Tag[7];

My Problem is now - Which is the easiest way to compare the two values ?
At the end I would like to make a comparision like this:

...
if (RFID_Tag == RFID_Tag_Card1)
     bla bla

But since the read out RFID_Tag is a char array and even if I typecast the char array with - (int)RFID_Tag , I still can't compare it with my stored value in the database (RIFD_Tag_Card1).

I hope my problem get's a bit clearer now.

I just don't know how to make the comparision without converting the char array into a int or a string or anything else :slight_smile:

Perhaps you guys have some suggestions

Thank you very much in advance

memcmp.

Hi,

thanks for the quick reply.

Can you explain me how to use / implement memcmp in my case with a little example ?

Greetings

int RFID_Tag_Card1 = 2232211347000 Have you really done that ? I assume that the missing semi-colon at the end is a typo but that aside, that number will not fit in an int.