Uint8_t to string

I want to take a variable in uint8_t format and convert it to hex and save it to string variable.
I wanted to do it this way. Found some promising infor on ToSting method

String received_id = "";
uint8_t received_char = 16;
void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  received_id=received_char.ToString("X");
  delay(2000);
}

But when I compile I get

request for member 'ToString' in 'received_char', which is of non-class type 'uint8_t {aka unsigned char}'

O this is just a basic example where I want "F" to be set as the value of received_id. In the program I want to read serial incoming data byte by byte and saving it to String variable in hex representation

received_char is a simple unsigned char, not a class, so it doesn't have member functions like "ToString".

I don't understand what you want to do.

char received_id[3];
sprintf(received_id, "%x", received_char);

This will result in the string 10 by the way, not f.

F is 15 btw

char hex[16] = {'0',…..'F'};
String received_id(hex[received_char]);

Still not fixed for me.
I am reading serial input byte by byte. I am sending each byte into the table and that works. I want to also create a variable which would hold the string of all bytes read in hex.

Let me give you the code.

  while(NFCserial.available()!=0){
    received_char = char (NFCserial.read());
    
    received_data[received_buf_pos] = received_char;
    tag_id += received_char; //adding all read bytes to a string. But this line adds decimal                
//representation and I want to have HEX
      }
    received_buf_pos++;

    }

To convert a byte to a hex string, use sprintf(). It's already mentioned in reply #4.

When you figure that out, just iterate through the source array and populate the destination (string) array. To append successive hex ASCII values to the destination string, you could use strcat().

If there is something you don't understand about that, you should explain.

Tried this. Still does not work

      char Byte_In_Hex;
      sprintf(Byte_In_Hex,"%x", received_char);
      tag_id += Byte_In_Hex; //adding to a string

No tag detected.
Searching new card...
No tag detected.
Searching new card...
I receive
2A7D128800028
And when I print Tag_id I get
40001288181254225924000
:frowning:

Byte_In_Hex needs to be a buffer, not a single char.
Didn't the compiler object to that?

I changed it to
char Byte_IN_HEX(3);
And now I get

2A7D128800028
Tag_ID: I get some funny charracters.
So still no joy

When you make a change to your code, we can't see it, so you need to show us.

Here is the code I have right now


uint8_t received_data[256];
uint8_t received_buf_pos;
uint8_t response_byte;
uint8_t data_len;
boolean received_complete;
String tag_id = "";
char Byte_In_Hex(3);
void serial_receive(){
  uint8_t received_char;
  
  while(NFCserial.available()!=0){
    received_char = char (NFCserial.read());

    //the first byte of the received message is the response
    if(received_buf_pos == 0){
      response_byte = received_char;
      }

    //second byte of the received data is the data length
    else if(received_buf_pos == 1){
      data_len = received_char;
      }

    else if(received_buf_pos == 2){
      
      }

    else if(received_buf_pos == 3){
      
      }
// Proximity card number starts at the fourth byte
    else{
      received_data[received_buf_pos-4] = received_char;
      sprintf(Byte_In_Hex,"%x", received_char);
      tag_id += Byte_In_Hex; //adding to a string
      }
    received_buf_pos++;

    if(received_buf_pos >= data_len){
      received_complete = true;
      }
    }
}

There are no prints in that code, so I don't know how you are seeing anything

Later in the code I have

Serial.println("Tag detected...");
    Serial.print("ID in HEX: ");
    for(int pos=7; pos>=0; pos--){
      Serial.print(received_data[pos], HEX);
      }
      Serial.println("");
    Serial.print("Tag_id: ");
    Serial.println(tag_id);

did you mean

char Byte_In_Hex[3];

?

Never post "snippets" - incomplete code. Please always post the complete sketch.

Then please post a complete, compilable example sketch instead of your full sketch.

Here you are. The brackets are changed
But still I get following output

Tag detected...
ID in HEX: 2A7D128800028
Tag_id: 2800808127d2a195c2800

#include <SoftwareSerial.h>
SoftwareSerial NFCserial(2, 3); //RX, TX

//const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8;


const int lock_pin = 2;

uint8_t echo_command[1] = {0x55};
uint8_t info_command[2] = {0x01, 0x00};
uint8_t detect_command_iso14443a_1[6] =  {0x09, 0x04, 0x68, 0x01, 0x07, 0x10};
uint8_t detect_command_iso14443a_2[6] =  {0x09, 0x04, 0x68, 0x01, 0x07, 0x00};
uint8_t detect_command_iso14443a_3[6] =  {0x02, 0x04, 0x02, 0x00, 0x02, 0x80};
uint8_t detect_command_iso14443a_4[6] =  {0x09, 0x04, 0x3A, 0x00, 0x58, 0x04};
uint8_t detect_command_iso14443a_5[6] =  {0x09, 0x04, 0x68, 0x01, 0x01, 0xD3};
uint8_t detect_command_iso14443a_6[4] =  {0x04, 0x02, 0x26, 0x07};
uint8_t detect_command_iso14443a_7[5] =  {0x04, 0x03, 0x93, 0x20, 0x08};


//uint8_t detect_command_iso14443a_6[4] =  {0x02, 0x02, 0x01, 0x0D};
//uint8_t detect_command_iso14443a_7[5] =  {0x04, 0x03, 0x26, 0x01, 0x00};


uint8_t received_data[256];
uint8_t received_buf_pos;
uint8_t response_byte;
uint8_t data_len;
boolean received_complete;
String tag_id = "";
char Byte_In_Hex[3];

struct tag{
   String Card;
   int Access;
};

tag Tags[3] = {{"xxxx",0},
               {"yyyy",1},
               {"zzzz",1}};

void serial_receive(){
  uint8_t received_char;
  
  while(NFCserial.available()!=0){
    received_char = char (NFCserial.read());

    //the first byte of the received message is the response
    if(received_buf_pos == 0){
      response_byte = received_char;
      }

    //second byte of the received data is the data length
    else if(received_buf_pos == 1){
      data_len = received_char;
      }

    else if(received_buf_pos == 2){
      
      }

    else if(received_buf_pos == 3){
      
      }
// Proximity card number starts at the fourth byte
    else{
      received_data[received_buf_pos-4] = received_char;
      sprintf(Byte_In_Hex,"%x", received_char);
      tag_id += Byte_In_Hex; //adding to a string
      }
    received_buf_pos++;

    if(received_buf_pos >= data_len){
      received_complete = true;
      }
    }
}


void scan_tag(){
  received_buf_pos = 0;
  received_complete = false;
  tag_id="";
  
  Serial.println("Searching new card...");
  NFCserial.write(detect_command_iso14443a_6, 4);
  delay(200);
  NFCserial.write(detect_command_iso14443a_7, 5);
  delay(300);

  if(NFCserial.available()) serial_receive();
  else return;
  if(response_byte == 0x80){
    Serial.println("Tag detected...");
    Serial.print("ID in HEX: ");
    for(int pos=7; pos>=0; pos--){
      Serial.print(received_data[pos], HEX);
      }
      Serial.println("");
    Serial.print("Tag_id: ");
    Serial.println(tag_id);    
    delay(2000); //this is important
    }
   else{
    Serial.println("No tag detected.");
    delay(2000);
    }
}


void setup() {  
  Serial.begin(9600);
  delay(1000);
  NFCserial.begin(57600);
  delay(1000);
  //Displaying RFID serial number
  Serial.println("RFID mudule:");
  NFCserial.write(info_command, 2);
  delay(100);
  show_serial_data();
  Serial.println("");
  delay(2000);
  //Initialising the module to read iso14443a proximity cards
  NFCserial.write(echo_command, 1);
  NFCserial.write(detect_command_iso14443a_1, 6);
  delay(200);
  NFCserial.write(detect_command_iso14443a_2, 6);
  delay(200);
  NFCserial.write(detect_command_iso14443a_3, 6);
  delay(200);
  NFCserial.write(detect_command_iso14443a_4, 6);
  delay(200);
  NFCserial.write(detect_command_iso14443a_5, 6);
  delay(1000);
  show_serial_data();
}


 
void loop() {  
  scan_tag();
 // for (int i=0;sizeof(Tags)-1; i++){
  //  delay(1000);
  //  Serial.println(Tags[i].Card); 
 // }
  
  //delay(100);
}

// this display the debug message
void show_serial_data()
{
  while(NFCserial.available()!=0)  /* If data is available on serial port */
  Serial.print(NFCserial.read(), HEX); /* Print character received on to the serial monitor */
}

Try again. Your cut and paste failed.

You are lying, this is nonsense and wouldn’t compile