Converting data from byte* to char

I'm stuck with my project, i am trying to send data over WiFi shield from my RFID reader. Reader reads data in byte, and Wifi shield needs data to be in char type in order to send it. I will post only the code that is relevant to this issue, since the whole project is pretty big.

char packetBuffer[255];
byte readCard[4]; 

if ( ! mfrc522.PICC_IsNewCardPresent()) {
                return;
        }
	
	if ( ! mfrc522.PICC_ReadCardSerial()) {
                return;
      	}

for (byte i = 0; i < mfrc522.uid.size; i++) {  // for size of uid.size write uid.uidByte to readCard
        readCard[i] = mfrc522.uid.uidByte[i];
        
    
        Serial.print(readCard[i], DEC);

       sprintf (packetBuffer, "%c", readCard);

       Serial.print(packetBuffer);

Serial.print(readCard*, DEC); ---- gives me the right card number (1329319791) but when i convert data, the data is bad (1213*
).
I am aware that this could probably be done using strings, but i have just started using Arduino and C++ so i lack knowledge and experience. Any help would be much appreciated.

       sprintf (packetBuffer, "%c", readCard);

What type of data does %c deal with? How many things of that type?

According to this, its character. C library function - sprintf(). And i guess its 11 characters long card number. :blush:

Yes, %c is to write ONE character to an array. Why do you need to use sprintf() to do that?

How is writing one character at a time to packetBuffer useful?

Where is the rest of the code? What does the receiving end code look like?

I wanted to use the sprintf() to convert data from byte type to char. This part of the code is for reading data from RFID tags, then it should convert data in order to be send using WiFi shield to another Arduino who will export that data to PC.

The rest of the code is working properly, this is the only part i need help with.

What's wrong with being simple

byte demoByte = 65;
char demoChar;

demoChar = (char) demoByte;

...R

Robin2:
What's wrong with being simple

byte demoByte = 65;

char demoChar;

demoChar = (char) demoByte;




...R

When i adapt your code to mine.

char packetBuffer[255];
byte readCard[4]; 

packetBuffer = (char)readCard;

i get this:

error: cast from 'byte*' to 'char' loses precision
error: incompatible types in assignment of 'char' to 'char [255]'

Daddy0:
When i adapt your code to mine.

char packetBuffer[255];

byte readCard[4];

packetBuffer = (char)readCard;




i get this: 

error: cast from 'byte*' to 'char' loses precision
error: incompatible types in assignment of 'char' to 'char [255]'

because it looks like you are trying to cast the whole array and they are different sizes as well...

char packetBuffer[255];
byte readCard[4] = {B00000001,B00000010,B00000011,B00000100}; 


void setup()
{
  Serial.begin(9600);
  packetBuffer[0] = (char)readCard[0];
  Serial.println(packetBuffer[0], BIN);
}

void loop()
{
  
}

if you want simple... use an array!

static int relay[16] = {31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46};

void setup()
{
  for (int i = 0; i < 16; i++)
  {
    pinMode(relay[i], OUTPUT); 
    digitalWrite(relay[i], LOW);
  }
  Serial.begin(115200); // open serial
  Serial.setTimeout(10);
  instructions();
}

void loop()
{
  while (Serial.available() > 0)
  {
    int cmd = Serial.parseInt();
    cmd = cmd - 1;
    if (cmd < 1 || cmd > 16)
    {
      Serial.println("Bad Value");
      instructions();
      break;
    }
    digitalWrite(relay[cmd], ! digitalRead(relay[cmd]));
    Serial.print("Relay on pin "); Serial.print(relay[cmd]); Serial.print(" just went "); Serial.println(digitalRead(relay[cmd]) == HIGH ? "HIGH" : "LOW");
    int relayCount = 0;
    for (int i = 0; i < 16; i++)
    {
      relayCount += (digitalRead(relay[cmd]) == HIGH) ? 1 : 0;
    }
    Serial.print("Total of "); Serial.print(relayCount); Serial.println(" relays ON");
    instructions();
  }
 }

void instructions()
{
  Serial.println("Press a number 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16  to toggle a relay on/off");
}

Ok, so this is how i did it, in case somebody will have the same issue. It turns byte to int first, then it turns int to char. For some reason functions sprintf and such were not working.

    byte readCard[4];
    char niz[12];
    int x0=0;                  
    int x1=0;                   
    int x2=0;                              
    int x3=0;   

    x3=(int) readCard[3];        
    x2=(int) readCard[2];         
    x1=(int) readCard[1];          
    x0=(int) readCard[0];     
        
    niz[11]=(x3 % 10)+48;
    x3=x3/10;
    niz[10]=(x3 % 10)+48;
    x3=x3/10;
    niz[9]=(x3 % 10)+48;
    x3=x3/10;
   
    niz[8]=(x2 % 10)+48;
    x2=x2/10;
    niz[7]=(x2 % 10)+48;
    x2=x2/10;
    niz[6]=(x2 % 10)+48;
    x2=x2/10;
  
    niz[5]=(x1 % 10)+48;
    x1=x1/10;
    niz[4]=(x1 % 10)+48;
    x1=x1/10;
    niz[3]=(x1 % 10)+48;
    x1=x1/10;
   
    niz[2]=(x0 % 10)+48;
    x0=x0/10;
    niz[1]=(x0% 10)+48;
    x0=x0/10;
    niz[0]=(x0 % 10)+48;
    x0=x0/10;
char packetBuffer[255];
byte readCard[4]; 

packetBuffer = (char)readCard;

Recall that an array name by itself is the lvalue of the array (i.e., where it is stored in memory). What the last statement does above is try to assign the base memory address of readCard[] to become the base memory address for packetBuffer[]. I think the error message would be more useful if it said something about trying to change a constant lvalue. Regardless, it's an error to try and reassign the base address of an array. Use pointers if you need to manipulate the contents of the arrays.

Wifi shield needs data to be in char type in order to send it.

Can you please provide some reference for this.

I understand that your code breaks down the bytes into three digits 0-9 expressed as char 48-57, but I can't understand why you need to do this. What values will the wifi shield actually transmit?

Byte is unsigned 8 bit value from 0 to 255 and if Char is a signed 8 bit data type from -128 to 127, but usually referenced to the positive ASCII 0-127 for readable output. are you saying that the wifi shield will not transmit an 8 bit number from 128-255? Will it transmit the full signed range of char? I would think that it is transmitting an 8 bit package and you should be able to get your byte values in there without having to break it down into individual digits.

1 Like