Byte array, convert/append to char array/string

I’m creating a project that uses Marc Boon’s RFIDuino code for an SL018 reader.

I have several hundred RFID tags, each with various properties about its identity written to blocks on the card. I’ve written some test sketches that will run a variety of functions according to values that are read off these specific stored blocks. I’d like to run a couple of if loops that are true if a code/string read from the card matches a specific condition. I’m having trouble storing the values read off the card into an array which can then be tested in the for loops. The problem is my uneducated coding - I’ve given this a pretty damn hard try, and have finally given in to asking for help!

In the RFIDuino examples (“sl018demo”), line 239 calls up a function from SL018.cpp:

printArrayAscii(rfid.getBlock(), 16);

This outputs to the serial terminal a string of ASCII characters that are the human-readable code I’d like to test my if loops against. An example output for block 4 might be “PASTA”. I’d ideally like something like:

if (storedBlock[4] == “PASTA” && storedBlock[5] == “ITALIAN”) { // code here }

The printArrayAscii function is:

void printArrayAscii(byte array[], byte len)
{
  for (byte i = 0; i < len;)
  {
    char c = array[i++];
    if (c < 0x20 || c > 0x7e)
    {
      Serial.print('.');
    }
    else
    {
      Serial.print(char(c));
    }
  }
}

I’m trying to convert the byte array taken from the rfid.getBlock() into a variable string, rather than printed output. I tried modifying the printArrayAscii to output the ASCII to a char array instead, but I just can’t get it to work. Help!

 char* getString(byte array[], byte len)
 {
 char concatString[len];
 for (byte i = 0; i < len;i++)
  {
  char c = array[i];
  
  if (c <= 0x30 || c >= 0x39) // 0-9, only want numbers and letters
   {
   concatString[i] = char(c);
   }
  else if (c <= 0x41 || c >= 0x5a) // A-Z, only want numbers and letters (capitals)
   { 

   concatString[i] = char(c);
   
   }
   else
   {
   concatString[i] = NULL;
   }
  }
  //Serial.println(concatString);
  return concatString;
 
 }

If I uncomment “Serial.println(concatString);” and run getString(rfid.getBlock(), 16) in the sl018demo, I get the expected ASCII printed out. If I then comment the print command, and try “Serial.println(getString(rfid.getBlock(), 16));” in sl018 demo, I get gibberish. I’ve tried variations of the byte to char conversion, including:

 char* getString(byte array[], byte len)
{
char concatString[len];
for (byte i = 0; i < len;i++)
  {
char c = array[i];
char temp[ 2 ];
temp[ 0 ] = c;
temp[ 1 ] = '\0';
strcpy (concatString,temp);
  }
  
  return concatString;
}

I’ve also tried to make concatString[len] = ‘\0’, which didn’t help either.

I was hoping that this would be fairly simple. I essentially have an array of bytes, that I’d like to convert into an ASCII string. This works when Serial.print is used, but I’d like to store the output in a variable for later use (store each string in an array(i) (square brackets, else gives italics) where i corresponds to the block that that string was read from). I thought that my “return concatString” would give me a char array[len] that contained the byte-char transformed characters - but no go. That said, why does the Serial.println(concatString) work?

My ultimate outcome:

storedBlock[block] = getString(rfid.getBlock(), 16); // where getString… returns the string to be stored in storedBlock[block]

 char* getString(byte array[], byte len)
 {
 char concatString[len];
 ...
  return concatString;

 }

An array is just a pointer to the first object in the array. You create this array locally, then return it. The problem is that the array is destroyed after the return call so that address that you returned is worthless.

void getString(byte array[], char buffer[], byte len) {
   // code to copy all the data into buffer
   // terminate the buffer
}

Then to call it, you would do this:

char buffer[16+1];
getString(rfid.getBlock(), buffer, 16);