Convert String to const unsigned char

String abc ="0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff";

const unsigned char displaydata[]={reinterpret_cast<const unsigned char*>(abc.c_str())};

display.drawBitmap(displaydata, startX, startY, bmpWidth, bmpHeight, GxEPD_WHITE);

Get error: invalid conversion from 'const unsigned char' to 'unsigned char' [-fpermissive]*

When I write like this

String abc ="0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff";

display.drawBitmap(reinterpret_cast<const unsigned char*>(abc.c_str()), startX, startY, bmpWidth, bmpHeight, GxEPD_WHITE);

No error but black screen no display image

When I write like this

const unsigned char displaydata[]={ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};

display.drawBitmap(displaydata, startX, startY, bmpWidth, bmpHeight, GxEPD_WHITE);

It is working

The string data coming from the server is returning to me as stirng, I am trying to reflect it on the screen. I want to convert a string data to const unsigned char with code

So I'm trying to convert

Go on. You can't convert an SRAM String to a const uint8_t* i.e. PROGMEM
It might work on a flat address space like ARM or ESP32 but it won't on a Harvard AVR.

However, drawBitmap() comes in several different flavours.
So you can just use abc.c_str() and it will use the SRAM flavour.

Untested. I am just assuming that the String is built dynamically at runtime. i.e. it is stored in SRAM.

David.

In this case, the compiler is doing the ASCII to hexidecimal conversion for you.

This creates a String composed of ASCII characters. Your code will have to parse out each hexidecimal number and convert to its binary equivalent, then store that in an appropriately sized array of unsigned char. Only then can you use drawBitmap to display the image.

You cannot display ASCII directly as a bitmap, as an example "0xff" should give FF in HEX, or binary 11111111, but as text gives you four bytes equivalent to 0x30 0x78 0x66 0x66, which are the HEX values of the ASCII characters.

That should do the conversion automagically. But you could also write:

display.drawBitmap((uint8_t*)abc.c_str(), startX, startY, bmpWidth, bmpHeight, GxEPD_WHITE);

David.

Edit. I am a little confused. Your drawBitmap() syntax is a little unexpected. Adafruit_GFX class has these methods :

  void drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w,
                  int16_t h, uint16_t color);
  void drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w,
                  int16_t h, uint16_t color, uint16_t bg);
  void drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h,
                  uint16_t color);
  void drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h,
                  uint16_t color, uint16_t bg);

So what library and what class did your display object come from?

1 Like

That's because what you wrote is a string of 52 characters. If you want a string of 9 characters, each with the value 0xFF:

String abc = "\xff\xff\xff\xff\xff\xff\xff\xff\xff";

Yes, the String contains different bytes e.g. " ", "0", "x"

drawBitmap() generally shows a monochrome bitmap. So "\xff\xff\xff\xff\xff\xff\xff\xff\xff"; would be 72 continuous GxEPD_WHITE pixels.

THe random bytes would show a bit pattern.

So as always, the OP needs to post a minimal compilable program to illustrate the problem. It would also reveal libraries used.

David.

const items can be put into PROGMEM, they are not automatically there.

I agree. But without knowing the "unusual" library or the target architecture, we don't know what the library code expects.

From the tried cast, I would deduct it expects a const uint8_t*,
and that cast would work, passing the ASCII as a bitmap.

Library : GxEPD.h

Sample Code

Image data comes to me as a string and I am looking for a way to display it with encoding.

Library : GxEPD.h

Ok, so you are using <GxEPD.h> and it contains a drawBitmap() method with different signature to the Adafruit_GFX methods.

First off. Run the GxEPD.h examples. Make sure that you are familiar with how to use the GxEPD.h methods.

Then post a sketch that shows how you are receiving the "String" and how you are building and storing the String.

I suspect that you are actually using an ESP32 target.
But why should I have to guess when you could just post a link to the actual hardware display?

With an ESP32 you should only need to say

abc.c_str()

David.

Hi David

when I write

display.drawBitmap((abc.c_str()), startX, startY, bmpWidth, bmpHeight, GxEPD_WHITE);

Get convert error message

When I wrote

display.drawBitmap(reinterpret_cast<const unsigned char*>(abc.c_str()), startX, startY, bmpWidth, bmpHeight, GxEPD_WHITE);

No error but just black screen

I use esp 32 and No matter what I do I can't get over this problem.

From GxEPD/GxEPD.h at master · ZinggJM/GxEPD · GitHub

    virtual void drawBitmap(const uint8_t *bitmap, uint32_t size, int16_t m = bm_normal) = 0; // monochrome

So it wants a const uint8_t* argument.

My suggestion in #4 should be changed to :

display.drawBitmap((const uint8_t*)abc.c_str(), startX, startY, bmpWidth, bmpHeight, GxEPD_WHITE);

I don't have any EPD displays. Jean-Marc is often on this Forum.
But the principal problem is "what is actually in your String" ?

It is much better to use a known pattern e.g. 0x55 or 0xF5. A string of 0xFF bytes is just going to produce solid pixels.

David.

Someone needs to teach the OP the difference between a textual representation of an initializer list for a character array, and a C++ String instance, and a bitmap in memory (be it in RAM or PROGMEM).
And the OP needs to tell exactly what the "String" he receives somehow looks like.
Maybe he needs to actually convert a textual representation, like the compiler does.
But maybe the data received is raw bitmap data.

Jean-Marc

OP has another topic for the same issue: Convert string to const unsigned char for display

Hi david

I try that but same just black screen :frowning:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.