Special character concatenation - HD44780

I have a class which holds two character strings: name and unit. I would like to store (char)0xDF and "C" together to form the degrees C symbol as a 'unit' inside a temperature object. e.g.

class myclass(char *myname="", char *myunit="")
{
strcpy(name, myname);
strcpy(unit, myunit);
int value;
}

I'd like to be able to do:

myclass temperature("Temperature: ", {(char)0xDF, "C"});

So that for a given myclass object I can perform something similar to:

lcd.print(myclassobject.name);
lcd.print(myclassobject.value);
lcd.print(myclasobject.unit);

But I cant add a literal array {(char)0xDF, "C"} as an argument. Seems they should be able to both be stored in a char array somehow? I'm trying to stay away from Strings if possible.

Why not just

myclass temperature("Temperature: ", "°C");

?

What happens if you try 0xA7?

How did you declare name and unit? You shouldn't use strcpy() unless you are sure the destination has enough room.

You can put 8-bit special characters in octal or hex: Octal: "\337" . (0xDF is 11 011 111) Hex: "\xDF"

See: https://en.cppreference.com/w/c/language/escape

johnwasser:
How did you declare name and unit? You shouldn’t use strcpy() unless you are sure the destination has enough room.

You can put 8-bit special characters in octal or hex:
Octal: “\337” . (0xDF is 11 011 111)
Hex: “\xDF”

See: Escape sequences - cppreference.com

  char name[16];
  char unit[6];

Yes I will have to be careful of that :o

What worked is:

strcpy(Temperature.unit,"\xDF");
strcat(Temperature.unit,"C");
lcd.print(Temperature.unit);

To print the unit °C.
But why can’t I give it both elements at once? It’s still a character array now. Why does {"\xDF",“C”} give ‘’ error? If its type issues why doesn’t const char *temp = {"\xDF",“C”} provide a feasible array of characters to strcpy?

guix:
Why not just

myclass temperature("Temperature: ", "°C");

?

I think its a different code to the LCD driver’s degree symbol.

syphex:
But why can’t I give it both elements at once?

You can if you create a custom character for the LCD. You have to set up grid of points to be on/off and transfer the eight bytes comprising the pattern to the display. Create a custom character with the letter ‘C’ and the degree symbol in one 5x8 matrix. This snippet transfers - creates - eight custom characters in the LCD.

char lcdPrintBufr[8]; // establish a buffer for delivery & pickup of custom char data
  for (byte i = 0; i < customChr_qty; i++) {
    memcpy_P (lcdPrintBufr, &customGrfx[i], customChr_s);
    lcd.createChar(i, lcdPrintBufr);
  }

Since the pattern is stored in the LCD , it’s not part of your output string. It’s printed by its own statement:

lcd.write(byte(rotateLeft_custom));

Try a site search on ‘custom character’.

syphex: What worked is:

strcpy(Temperature.unit,"\xDF");
strcat(Temperature.unit,"C");
lcd.print(Temperature.unit);

But why can't I give it both elements at once?

You can, but to pass them as a string you have to put them in a string:

strcpy(Temperature.unit,"\xDFC");
lcd.print(Temperature.unit);

Or, because adjacent string constants are merged:

strcpy(Temperature.unit,"\xDF" "C");
lcd.print(Temperature.unit);