Compose a string from char

Hi all.

I'm trying a string compose from characters.

I have:

char buf[15] = " ";
string id = "";

In a for, buf is getting these values in each execution: 226, 128, 104, 016, 000, 000, 000, 057, 130, 242, 031, 163.

The idea is that the id string be: 226128104016000000000057130242031163, but I can't get it. As far I went, id has been 226128104016, but it stopped when zeros came, or something I don't know.

I don't know how to get it.

Thanks in advance.

Try this:
id+=String.valueOf(buf);

Hope this helps.

Please post your full sketch. If possible you should always post code directly in the forum thread as text using code tags (</> button on the toolbar). This will make it easy for anyone to look at it, which will increase the likelihood of you getting help. If the sketch is longer than the forum will allow then it’s ok to add it as an attachment.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor then you will not have access to this useful tool. I recommend using the standard Arduino IDE instead.

When your code requires a library that’s not included with the Arduino IDE please always post a link(using the chain links icon on the toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger(Sketch > Include Library > Manage Libraries) then say so and state the full name of the library.

Keep in mind that a C string is a null terminated char array, null being the number 0. That’s different from the character ‘0’.

Senen:
Hi all.

I'm trying a string compose from characters.

I have:

char buf[15] = " ";
string id = "";

In a for, buf is getting these values in each execution: 226, 128, 104, 016, 000, 000, 000, 057, 130, 242, 031, 163.

The idea is that the id string be: 226128104016000000000057130242031163, but I can't get it. As far I went, id has been 226128104016, but it stopped when zeros came, or something I don't know.

I don't know how to get it.

Thanks in advance.

I believe you need to convert 226, for example, to a string and then concatenate it to buf, which is a 'C' string if you want it to be.

But this is probably easier for what you want.

String buf;

buff += String(226);

buf will then contain the text string "226".

If you want to keep buf as a 'C' string then do this:

char buff[15];

strcat(buf, String(226));

You need to convert the integer value 226 etc to readable text consisting of 3 ASCII digits.

Google ASCII table.

Anshu_Raj:
Try this:
id+=String.valueOf(buf);

Hope this helps.

Opps! my bad seems like this one doesn't works on arduino.

Try this hope this works:

String id((char*)buf);

Oh and buf will need to be a lot bigger than 15 element.

Count up the digits in that long string you want it to contain “226…”, add 1 to it (for the NULL or end of string terminator) and declare your array of that size.

char buf[64] for example where 64 is the number of digits plus 1.

/*
  Reading multiple RFID tags, simultaneously!
  By: Nathan Seidle @ SparkFun Electronics
  Date: October 3rd, 2016
  https://github.com/sparkfun/Simultaneous_RFID_Tag_Reader

  Constantly reads and outputs any tags heard

  If using the Simultaneous RFID Tag Reader (SRTR) shield, make sure the serial slide
  switch is in the 'SW-UART' position
*/

#include <SoftwareSerial.h> //Used for transmitting to the device

SoftwareSerial softSerial(2, 3); //RX, TX

#include "SparkFun_UHF_RFID_Reader.h" //Library for controlling the M6E Nano module
RFID nano; //Create instance

String id;

void setup()
{
  Serial.begin(115200);
  while (!Serial); //Wait for the serial port to come online

  if (setupNano(38400) == false) //Configure nano to run at 38400bps
  {
    Serial.println(F("Module failed to respond. Please check wiring."));
    while (1); //Freeze!
  }

  nano.setRegion(REGION_EUROPE); //Set to North America

  nano.setReadPower(500); //5.00 dBm. Higher values may caues USB port to brown out
  //Max Read TX Power is 27.00 dBm and may cause temperature-limit throttling

  Serial.println(F("Press a key to begin scanning for tags."));
  while (!Serial.available()); //Wait for user to send a character
  Serial.read(); //Throw away the user's character

  nano.startReading(); //Begin scanning for tags
}

void loop()
{
  if (nano.check() == true) //Check to see if any new data has come in from module
  {
    byte responseType = nano.parseResponse(); //Break response into tag ID, RSSI, frequency, and timestamp

    if (responseType == RESPONSE_IS_KEEPALIVE)
    {
      Serial.println(F("Scanning"));
    }
    else if (responseType == RESPONSE_IS_TAGFOUND)
    {
      //If we have a full record we can pull out the fun bits
      int rssi = nano.getTagRSSI(); //Get the RSSI for this tag read

      long freq = nano.getTagFreq(); //Get the frequency this tag was detected at

      long timeStamp = nano.getTagTimestamp(); //Get the time this was read, (ms) since last keep-alive message

      byte tagEPCBytes = nano.getTagEPCBytes(); //Get the number of bytes of EPC from response

      //Print EPC bytes, this is a subsection of bytes from the response/msg array
      Serial.print(F(" epc["));
      for (byte x = 0 ; x < tagEPCBytes ; x++)
      {
        if (nano.msg[31 + x] < 0x10) Serial.print(F("0")); //Pretty print
        char buf[15]=" ";
        char* formato=" ID %03i";
        Serial.print(nano.msg[31 + x], HEX);
        int i = nano.msg[31 + x];
        sprintf(buf, formato, i);
        Serial.print(buf);
        Serial.println();
      }
      Serial.print(F("ID ES: "));
      Serial.println(id);
      Serial.print(F("]"));

      Serial.println();
    }
    else if (responseType == ERROR_CORRUPT_RESPONSE)
    {
      Serial.println("Bad CRC");
    }
    else
    {
      //Unknown response
      Serial.print("Unknown error");
    }
  }
}

//Gracefully handles a reader that is already configured and already reading continuously
//Because Stream does not have a .begin() we have to do this outside the library
boolean setupNano(long baudRate)
{
  nano.begin(softSerial); //Tell the library to communicate over software serial port

  //Test to see if we are already connected to a module
  //This would be the case if the Arduino has been reprogrammed and the module has stayed powered
  softSerial.begin(baudRate); //For this test, assume module is already at our desired baud rate
  while(!softSerial); //Wait for port to open

  //About 200ms from power on the module will send its firmware version at 115200. We need to ignore this.
  while(softSerial.available()) softSerial.read();
  
  nano.getVersion();

  if (nano.msg[0] == ERROR_WRONG_OPCODE_RESPONSE)
  {
    //This happens if the baud rate is correct but the module is doing a ccontinuous read
    nano.stopReading();

    Serial.println(F("Module continuously reading. Asking it to stop..."));

    delay(1500);
  }
  else
  {
    //The module did not respond so assume it's just been powered on and communicating at 115200bps
    softSerial.begin(115200); //Start software serial at 115200

    nano.setBaud(baudRate); //Tell the module to go to the chosen baud rate. Ignore the response msg

    softSerial.begin(baudRate); //Start the software serial port, this time at user's chosen baud rate
  }

  //Test the connection
  nano.getVersion();
  if (nano.msg[0] != ALL_GOOD) return (false); //Something is not right

  //The M6E has these settings no matter what
  nano.setTagProtocol(); //Set protocol to GEN2

  nano.setAntennaPort(); //Set TX/RX antenna ports to 1

  return (true); //We are ready to rock
}

This skecth is from a RFID reader, so, each tag has different ID, each time I need the ID (id) string from different multiple char (buf).

I will try what you recommend in your posts, Thanks!

each time I need the ID (id) string

Why do you think you need the bytes as a string or String? I'm almost certain that you don't.

PaulS:
Why do you think you need the bytes as a string or String? I’m almost certain that you don’t.

Because, why not? :smiley: :smiley: :smiley: :smiley: :wink:

Anshu_Raj:
Because, why not? :smiley: :smiley: :smiley: :smiley: :wink:

Why not represent 12 bytes as 36 characters? I can't think of a single reason not to piss away memory like that. I withdraw the question.

PaulS:
Why not represent 12 bytes as 36 characters? I can't think of a single reason not to piss away memory like that. I withdraw the question.

I simply need an id from each RFID tag read to send this id. I need all the buf together. I thought string as method.

I have solved it. You can see how here:

Many thanks for your effort