Skip to main content
Tweeted twitter.com/StackArduino/status/1290618504786386944
Code formatting
Source Link
Greenonline
  • 3.2k
  • 7
  • 37
  • 49

I'm reading a RFID Card from RC522 and send the data to my computer. My problem is the conversion of byte[] to String or std::string.

#define SIZE_BUFFER     18
#define MAX_SIZE_BLOCK  16

byte buffer[SIZE_BUFFER] = {0};
byte tam = SIZE_BUFFER;

// get data
status = mfrc522.MIFARE_Read(bloco, buffer, &tam);

  Serial.print(F("\nDados bloco ["));
  Serial.print(F("]: "));

// printing...
 for (uint8_t i = 0; i < MAX_SIZE_BLOCK; i++)
  {
      Serial.write(buffer[i]);
      str += String(buffer[i]);
  }

OUTPUT:

Dados bloco []: asdas

So, It'sit's ok! But if I try make a string:

  String str = "";
  
  for (uint8_t i = 0; i < MAX_SIZE_BLOCK; i++)
  {
      str += String(buffer[i]);
  }
  Serial.println(" ");
  Serial.println(str);

The output is:

97115100971153232323232323232323232

HowWhat is the correctelycorrect way to create a stringString from byte[] byte[]?

I'm reading a RFID Card from RC522 and send the data to my computer. My problem is the conversion of byte[] to String or std::string.

#define SIZE_BUFFER     18
#define MAX_SIZE_BLOCK  16

byte buffer[SIZE_BUFFER] = {0};
byte tam = SIZE_BUFFER;

// get data
status = mfrc522.MIFARE_Read(bloco, buffer, &tam);

  Serial.print(F("\nDados bloco ["));
  Serial.print(F("]: "));

// printing...
 for (uint8_t i = 0; i < MAX_SIZE_BLOCK; i++)
  {
      Serial.write(buffer[i]);
      str += String(buffer[i]);
  }

OUTPUT:

Dados bloco []: asdas

So, It's ok! But if I try make a string:

  String str = "";
  
  for (uint8_t i = 0; i < MAX_SIZE_BLOCK; i++)
  {
      str += String(buffer[i]);
  }
  Serial.println(" ");
  Serial.println(str);

The output is:

97115100971153232323232323232323232

How is the correctely way to create a string from byte[] ?

I'm reading a RFID Card from RC522 and send the data to my computer. My problem is the conversion of byte[] to String or std::string.

#define SIZE_BUFFER     18
#define MAX_SIZE_BLOCK  16

byte buffer[SIZE_BUFFER] = {0};
byte tam = SIZE_BUFFER;

// get data
status = mfrc522.MIFARE_Read(bloco, buffer, &tam);

  Serial.print(F("\nDados bloco ["));
  Serial.print(F("]: "));

// printing...
 for (uint8_t i = 0; i < MAX_SIZE_BLOCK; i++)
  {
      Serial.write(buffer[i]);
      str += String(buffer[i]);
  }

OUTPUT:

Dados bloco []: asdas

So, it's ok! But if I try make a string:

  String str = "";
  
  for (uint8_t i = 0; i < MAX_SIZE_BLOCK; i++)
  {
      str += String(buffer[i]);
  }
  Serial.println(" ");
  Serial.println(str);

The output is:

97115100971153232323232323232323232

What is the correct way to create a String from byte[]?

deleted 36 characters in body
Source Link
Augusto
  • 131
  • 1
  • 1
  • 5

I'm reading a RFID Card from RC522 and send the data to my computer. My problem is the conversion of byte[] to String or std::string.

#define SIZE_BUFFER     18
#define MAX_SIZE_BLOCK  16

byte buffer[SIZE_BUFFER] = {0};
byte tam = SIZE_BUFFER;

// get data
status = mfrc522.MIFARE_Read(bloco, buffer, &tam);

  Serial.print(F("\nDados bloco ["));
  Serial.print(F("]: "));

// printing...
 for (uint8_t i = 0; i < MAX_SIZE_BLOCK; i++)
  {
      Serial.write(buffer[i]);
      str += String(buffer[i]);
  }

OUTPUT:

Dados bloco []: asdas

So, It's ok! But if I try make a string:

  String str = "";
  
  for (uint8_t i = 0; i < MAX_SIZE_BLOCK; i++)
  {
      Serial.write(buffer[i]);
      str += String(buffer[i]);
  }
  Serial.println(" ");
  Serial.println(str);

The output is:

97115100971153232323232323232323232

How is the correctely way to create a string from byte[] ?

I'm reading a RFID Card from RC522 and send the data to my computer. My problem is the conversion of byte[] to String or std::string.

#define SIZE_BUFFER     18
#define MAX_SIZE_BLOCK  16

byte buffer[SIZE_BUFFER] = {0};
byte tam = SIZE_BUFFER;

// get data
status = mfrc522.MIFARE_Read(bloco, buffer, &tam);

  Serial.print(F("\nDados bloco ["));
  Serial.print(F("]: "));

// printing...
 for (uint8_t i = 0; i < MAX_SIZE_BLOCK; i++)
  {
      Serial.write(buffer[i]);
      str += String(buffer[i]);
  }

OUTPUT:

Dados bloco []: asdas

So, It's ok! But if I try make a string:

  String str = "";
  
  for (uint8_t i = 0; i < MAX_SIZE_BLOCK; i++)
  {
      Serial.write(buffer[i]);
      str += String(buffer[i]);
  }
  Serial.println(" ");
  Serial.println(str);

The output is:

97115100971153232323232323232323232

How is the correctely way to create a string from byte[] ?

I'm reading a RFID Card from RC522 and send the data to my computer. My problem is the conversion of byte[] to String or std::string.

#define SIZE_BUFFER     18
#define MAX_SIZE_BLOCK  16

byte buffer[SIZE_BUFFER] = {0};
byte tam = SIZE_BUFFER;

// get data
status = mfrc522.MIFARE_Read(bloco, buffer, &tam);

  Serial.print(F("\nDados bloco ["));
  Serial.print(F("]: "));

// printing...
 for (uint8_t i = 0; i < MAX_SIZE_BLOCK; i++)
  {
      Serial.write(buffer[i]);
      str += String(buffer[i]);
  }

OUTPUT:

Dados bloco []: asdas

So, It's ok! But if I try make a string:

  String str = "";
  
  for (uint8_t i = 0; i < MAX_SIZE_BLOCK; i++)
  {
      str += String(buffer[i]);
  }
  Serial.println(" ");
  Serial.println(str);

The output is:

97115100971153232323232323232323232

How is the correctely way to create a string from byte[] ?

Source Link
Augusto
  • 131
  • 1
  • 1
  • 5

Problems on convert byte[] to String

I'm reading a RFID Card from RC522 and send the data to my computer. My problem is the conversion of byte[] to String or std::string.

#define SIZE_BUFFER     18
#define MAX_SIZE_BLOCK  16

byte buffer[SIZE_BUFFER] = {0};
byte tam = SIZE_BUFFER;

// get data
status = mfrc522.MIFARE_Read(bloco, buffer, &tam);

  Serial.print(F("\nDados bloco ["));
  Serial.print(F("]: "));

// printing...
 for (uint8_t i = 0; i < MAX_SIZE_BLOCK; i++)
  {
      Serial.write(buffer[i]);
      str += String(buffer[i]);
  }

OUTPUT:

Dados bloco []: asdas

So, It's ok! But if I try make a string:

  String str = "";
  
  for (uint8_t i = 0; i < MAX_SIZE_BLOCK; i++)
  {
      Serial.write(buffer[i]);
      str += String(buffer[i]);
  }
  Serial.println(" ");
  Serial.println(str);

The output is:

97115100971153232323232323232323232

How is the correctely way to create a string from byte[] ?