Go Down

Topic: [example code & question] replacing (Unicode-) Chars for Display (Read 1 time) previous topic - next topic

_frank_

Hi,
i'm accessing a DIP204-4 via 4-bit-mode. i had to replace some chars (unicode-chars are most difficult)

here my current code (maybe someone needs it):
Code: [Select]
void ChangeUnicode(char *text,int pos,char c)
{
  text[pos]=c;
for (int j=pos+1;j<strlen(text)-1;j++)
{
text[j]=text[j+1];
}
text[strlen(text)-1]='\0';
}


void ChangeChars(char *text)
{
  for (int i=0;i<strlen(text);i++)
  {
    //char hexcode[4];
    //sprintf(hexcode,"%x",text[i]);
    //Serial.println(hexcode);
    if ((text[i]==0xFFC2) || (text[i]==0xFFC3))
    {//handling unicode
        //Serial.println("handling Unicode...");
switch(text[i])
{
  case 0xFFC3:
  {
    switch (text[i+1])
    {
      case 0xFF84: ChangeUnicode(text,i,0x5B); break;//Ä
      case 0xFF96: ChangeUnicode(text,i,0x5C); break;//Ö
      case 0xFF9C: ChangeUnicode(text,i,0x5E); break;//Ü
              case 0xFFA4: ChangeUnicode(text,i,0x7B); break;//ä
      case 0xFFB6: ChangeUnicode(text,i,0x7C); break;//ö
      case 0xFFBC: ChangeUnicode(text,i,0x7E); break;//ü
      case 0xFF9F: ChangeUnicode(text,i,0xBE); break;//ß
              //C3 88 C3 89 C3 8A C3 8B (ÈÉÊË) => diagonal arrows (ul:16,ur:17,dl:18dr:19)
              case 0xFF88: ChangeUnicode(text,i,0x16); break;//È => ul
              case 0xFF89: ChangeUnicode(text,i,0x17); break;//É => ur
              case 0xFF8A: ChangeUnicode(text,i,0x18); break;//Ê => dl
              case 0xFF8B: ChangeUnicode(text,i,0x19); break;//Ë => dr
              //C3 92 C3 93 C3 94 C3 95 (ÒÓÔÕ) => straight arrows (u:DE,r:DF,d:E0,l:E1)
              case 0xFF92: ChangeUnicode(text,i,0xDE); break;//Ò => u
              case 0xFF93: ChangeUnicode(text,i,0xDF); break;//Ó => r
              case 0xFF94: ChangeUnicode(text,i,0xE0); break;//Ô => d
              case 0xFF95: ChangeUnicode(text,i,0xE1); break;//Õ => l
      }
    }break;
    case 0xFFC2:
    {
      switch (text[i+1])//todo: remove i+1
      {
        case 0xFFBD: ChangeUnicode(text,i,0x8A); break;//1/2
        case 0xFFBC: ChangeUnicode(text,i,0x8B); break;//1/4
        case 0xFFB5: ChangeUnicode(text,i,0x8F); break;//µ
      }
    }break;
}
    } else
    {
  switch(text[i])
  {
    case '{': text[i]=0xFD; break;
    case '}': text[i]=0xFF; break;
    case '$': text[i]=0xA2; break;
    case '@': text[i]=0xA0; break;
    case '[': text[i]=0xFA; break;
    case ']': text[i]=0xFC; break;
  }
    }
  }
}

void ShowTextOnDisplay(int row,int col, char *text)
{
  lcd.setCursor(col,row);
  //now replacing wrong chars
  ChangeChars(text);

  lcd.print(text);
}


this code works like a charm...but why is text 2 bytes (i tested using print out the hexcode of "char", see code above)?

regards Frank

dc42


this code works like a charm...but why is text 2 bytes (i tested using print out the hexcode of "char", see code above)?


You were using the %x format specifier to print the hex code, that that format specifier expects an integer. When you pass a character to a varargs function such as printf, that parameter is subject to integral promotion. So what you actually printed was the value of the character promoted to a 2-byte integer. Evidently, 'char' is signed when using this compiler and platform, hence for a character with a value of 0x80 or greater, the value of the most significant byte in the promoted value is 0xFF.

Your code should work just as well if you replace e.g. the integer literal 0xFFC3 by the character literal '\xC3' and similarly for your other constants.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

_frank_

#2
Sep 29, 2011, 01:34 pm Last Edit: Sep 29, 2011, 01:37 pm by _frank_ Reason: 1
why is char-type signed (maybe -127..0..128)? imho a char (byte) cannot be negative...negative is interpreted by datatype
how  can i convert it to be C3 instead of FFC3 (cast to int?)?

dc42


why is char-type signed (maybe -127..0..128)?


The C and C++ standards allow 'char' to be either signed or unsigned. The compiler has to choose one of them.


how  can i convert it to be C3 instead of FFC3?


It already is C3, it's only when it gets implicitly promoted to 'int' that it becomes FFC3. If you want it to get promoted to 00C3 instead, cast the value converned to 'unsigned char' at the point where it gets promoted, for example:

Code: [Select]

sprintf(hexcode,"%x", (unsigned char)(text[i]));
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Go Up