Go Down

Topic: How to convert char to unsigned char? (Read 2 times) previous topic - next topic

pc007

Oct 17, 2010, 04:45 am Last Edit: Oct 17, 2010, 04:47 am by pc007 Reason: 1
I have an LCD display using the ST7920 controller and I am using the dfrobot serial library available here --> http://www.dfrobot.com/image/data/FIT0021/LCD12864RSPI.rar

I'm simply trying to write a number on to the LCD screen but having a little trouble as the function to write text to the screen requires unsigned char.

I can use the following to convert a number to a string.
   char millis_char[16];
   unsigned long millis_ulong = millis();
   sprintf(millis_char,"%d",millis_ulong);


but I don't know how to convert the string to unsigned char.

this is to use in the LCDA.DisplayString function

any ideas?


Korman

Char and unsigned char convert themselves from one to the other, so no trouble there.

To access a single digit in your array, you just index it. For example to pass digit 3 (the numbering starts as 0) to a magic_display_function is:
Code: [Select]
magic_display_function (millis_char[3]);


Korman

davekw7x

#2
Oct 17, 2010, 08:16 pm Last Edit: Oct 17, 2010, 09:40 pm by davekw7x Reason: 1
Quote
char to unsigned char?


You can tell the compiler to treat the array as unsigned char.  (Use a cast)
Code: [Select]

  char millis_char[16];
  unsigned long millis_ulong = millis();
  sprintf(millis_char,"%lu", millis_ulong); // Works OK: sprintf uses char arrays.
  LCDA.DisplayString(0, 0, (unsigned char *)millis_char, 16);// The LCDA function requires unsigned char arrays.

Regards,

Dave

Footnote:

Not related to your question, but Something That It Is Good To Know:

Note that the format specifier "%d" is used for ints.

For long ints use "%ld"
For unsigned long ints use "%lu"

pc007

Thanks davekw7x.  Exactly the info I was looking for.

Go Up