I've been working on a small project getting some images displayed in a Nokia 5110 screen via an Arduino. I've been getting it working, but one of the issues I have is that I would like to have a list on integers and convert that an unsigned char array that the display requires. Generally I have something like this which works out of the box.

unsigned char src [] PROGMEM = {0x00, 0x00}

Surprisingly, I found that this also works

unsigned char src [] PROGMEM = {255, 255}

But when I have a list like this

int src [] = {255,255}

and try to convert it to an unsigned char array, things don't display correctly

unsigned char new_src[2];

for (int i=0; i<2; i++){
    new_src[i] = (unsigned char) src[i];

When I try to display this, it's clearly broken. I thought simply casting the int to an unsigned char would be enough (it doesn't work even if I don't cast it).

Here's how I display the array (for 84x48, the above arrays were just an example) :

Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
display.drawBitmap(0, 0,  src, 2, 48, BLACK);

On more note:

The end game is to have a file with a list of ints. I would then read that list of ints into an array and convert it into an unsigned char array and then display that.

  • How do you display src? Apr 8 at 17:39
  • @EdgarBonet edited into the original post
    – Snuh
    Apr 8 at 17:41
  • 1. What do you mean by “works”? 2. Why arrays of length 2? For an 84×48 bitmap you need an array of 528 bytes. 3. Does it still work if the PROGMEM arrays are const? 4. Why do you want to have a list of int? Other than wasting RAM, what does that give you that a list of uint8_t doesn't? Apr 8 at 19:19
  • 1. "works" as in displays the image properly. 2. This was just an example for simplicity sake. 3. Yes it still works. 4. Because I'm reading the integer values out of file. 4. My C skills aren't as sharp as your so I'm sorry for wasting RAM. @EdgarBonet
    – Snuh
    Apr 8 at 20:53
  • Besides mentioning it, I don't think @EdgarBonet's point was the extra RAM (or Flash), but on the fact that storing your data as 'unsigned char src [] PROGMEM' works for you - why do not use that?
    – JRobert
    Apr 8 at 21:14

The idea is that I would read a list of ints from a file ( I have several files that I would iterate through) and convert that list to the unsigned char array on each loop iteration.

The file could be a stream of bytes just as well as a stream of ints (or uints). If it were, no conversion would need to be done - just read a byte & store it, read a byte & store it, .... If instead, it is a character representation of values, "37, 150, 8, ...", again, read the characters of one value, convert them to int8_t or uint8_t and store them in your byte array. Look up the atoi() or sscanf() functions to do the conversion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.