How can i use strcat function on byte?

I want to display the EEPROM byte value on LCD.. My question is is byte a string? How can i use strcat function for byte?

No, but you can still print it using lcd.print:

LiquidCrystal lcd(... /* pin numbers here */);
...
  byte b = EEPROM.read(0);
  lcd.print(b, HEX);

Thank you.. I want to make a function that collect all variable data using strcat.. but i dont know how to use strcat with that byte value:

The following Mpwm variable is byte? how can i use byte here?????????????????????

strcat(menu.sbuf,"\nMpwm:");strcat(menu.sbuf,???????,buf,10));

If you are trying to copy a string to the end of a byte, you can't. You need to put the byte into a null terminated char array. If you are trying to add a byte to the end of a null terminated char array then you can do this (assuming there is enough space in the array):

index = strlen(somestring);
somestring[index++] = somebyte;
somestring[index] = '\0';

This is assuming you don't have index, which isn't usually the case

You can concatenate a byte onto the end of a string, using strcat(). If you are having problems doing so, you should tell us what they are.

Most likely, you have to lie to the compiler, telling it that the byte really is a char (using a cast).

PaulS,
Okay… I am displaying variable parameters on 20x4 character LCD screen. There is only one byte variable M_PWM that store the values between 20 to 120. The rest are all integers…
Following is my half backed function:

void msc(){
  static  char buf[7];
  int Diff=0;
  //1st lcd line
  strcpy(menu.sbuf,"E:"); strcat(menu.sbuf,itoa((int)(READ_RIGHT_LDR),buf,10)); strcat(menu.sbuf,"W:"); strcat(menu.sbuf,itoa((int)(READ_LEFT_LDR),buf,10));  //integer variable READ_RIGHT_LDR and READ_LEFT_LDR getting values from Analog pins (1~1023)
  if (READ_RIGHT_LDR>READ_LEFT_LDR)
  {
    Diff=READ_RIGHT_LDR-READ_LEFT_LDR;
   }
   else {
     Diff=READ_LEFT_LDR-READ_RIGHT_LDR;
     }   
  strcat(menu.sbuf,"Diff:"); strcat(menu.sbuf,itoa((int)(Diff),buf,10));
  //2nd LCD Line
  strcat(menu.sbuf,"\nMpwm:");strcat(menu.sbuf,M_PWM,buf,10));  //M_PWM is a byte range from 20 to 120. Here i am stuck....how can i use strcat??
 
 // CALCULATING MINUTES AND SECOND
  int seconds = (int) ( CountdownTime/ 1000) % 60 ;  //CountdownTime is unsigned long  and a global variable suppose its getting values from Millis()
  int minutes = (int) ((CountdownTime / (1000*60)) % 60);
  strcat(menu.sbuf," CNTD:");strcat(menu.sbuf,itoa((int)(minutes),buf,10)); strcat(menu.sbuf,":");strcat(menu.sbuf,itoa((int)(seconds),buf,10));
  //3rd LCD Line
  strcat(menu.sbuf,"\nSamplingTime: ");strcat(menu.sbuf,itoa((int)(SampleTime),buf,10));//SampleTime is integer variable
    //4th LCDLine
  strcat(menu.sbuf,"\nSensitivity: ");strcat(menu.sbuf,itoa((int)(Sensitivity),buf,10));//Sensitivity is also integer
  menu.drawUsrScreen(menu.sbuf);
  }
strcat(menu.sbuf,M_PWM,buf,10));

I don't know what you think this is doing, and apparently this is where the error occurs. The strcat() function only takes two arguments - where to copy from and where to copy to. menu.sbuf is obviously where to copy to, M_PWM is where to copy from. But, what all the rest of that stuff is is a mystery.

It looks like you are missing a call to itoa() in that call. That's one reason I don't think nested calls are a good idea.

But, do explain what you are trying to do, there, and we can figure out how to make that possible.

Thanks PaulS, strcat(menu.sbuf,M_PWM,buf,10));...i know this is wrong...Actually in this code i want to ask how can i use strcat with byte variable M_PWM? can you please share how strcat can be used for byte variable?

brunitali, in one of thread gave this solution,

//By the way the itoa works fine also with byte, without casting.

  byte a=78;
  char buf[4];
  Serial.print(itoa(a, buf,10));

but i dont know why he defined the char buf[4]; ...why 4?

but i dont know why he defined the char buf[4]; ...why 4?

Because a byte can only represent three decimal digits.

AWOL: Because a byte can only represent three decimal digits.

Thanks AWOL, suppose the M_PWM value stored is 100. Can i use

   char buff[4];
strcat(menu.sbuf,"\nMpwm:");strcat(menu.sbuf,itoa(M_PWM,buff,10));

will this be Mpwm:100

Do you think all that will fit in four bytes? If you can get 255 in such a buffer, what makes you wonder whether 100 will fit?

static  char buf[7];
strcat(menu.sbuf,"\nMpwm:");strcat(menu.sbuf,itoa(M_PWM,buf,10));

i am using MENWIZ library and i think menu.sbuf is taking care off...?

The string "100" occupies four bytes, one byte for each of the characters and one extra for the terminating null. There's no need for seven bytes.

I am sorry i am totally knocked out in this...:( :~ being a mechanical engineer by proffession i am totally newborn in programming..pardon me if i am asking too much...

100 is a three digit number. 255 is a three digit number. To represent them in ASCII requires one byte per digit. To represent them as a C string needs three bytes plus a null terminator, making four bytes. An Arduino int can represent at most a five digit decimal number, requiring six bytes to represent as a C string.

Edit: oops, forgot the sign. Make that seven bytes for the "int"

Thank you AWOL for explanation… i shall try my sketch and shall post the results… :slight_smile:

I am sorry i am totally knocked out in this…

You have the Serial.print() method and the Serial Monitor application. Add some Serial.print() statements and open the Serial Monitor. See what is happening. There is no reason to guess/speculate/pull your hair out. The Arduino will tell you, if you ask the right questions.

Thanks PaulS,AWOL and all other contributors... The sketch is working now... I learn a lot in this thread.. I wish i have a intelligibly and memory like you all.. :) Regards

PS: Mayan Calander.. Now we know why the Mayan on the calendar has his tongue sticking out. The whole dang thing was a “GOT’CHA” joke.....

Okay here is the use of strcat for byte type variable. This working fine.

strcat(sbuf,itoa(M_PWM,buf,10));