Pages: [1]   Go Down
Author Topic: How to convert char to unsigned char?  (Read 2392 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

« Last Edit: October 16, 2010, 09:47:29 pm by pc007 » Logged

Central Europe
Offline Offline
Edison Member
*
Karma: 7
Posts: 1220
Use the Source, Luke.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
magic_display_function (millis_char[3]);

Korman
Logged

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 5
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
char to unsigned char?

You can tell the compiler to treat the array as unsigned char.  (Use a cast)
Code:
  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"
« Last Edit: October 17, 2010, 02:40:43 pm by davekw7x » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks davekw7x.  Exactly the info I was looking for.
Logged

Pages: [1]   Go Up
Jump to: