String abc ="0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff"
const unsigned char displaydata={ '" abc "'};
display.drawBitmap(displaydata, startX, startY, bmpWidth, bmpHeight, GxEPD_WHITE);
This is not display but
const unsigned char displaydata={ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
display.drawBitmap(displaydata, startX, startY, bmpWidth, bmpHeight, GxEPD_WHITE);
This is working and displayed
I need convert a string to const unsigned char with code
is it possible ?
The data coming from the server is returning to me as stirng, I am trying to reflect it on the screen.
So I'm trying to convert
CodePudding user response:
'" abc "'
is an illegal character literal. You can't build an array like that.What looks like an array in your second example
const unsigned char displaydata={ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
is in fact only one
const unsigned char
. It should be:const unsigned char displaydata[]={ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
You can probably define your String
like this:
String abc = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
Note that the encoding above matches your second example:
const unsigned char displaydata[] = { 0xff, 0xff, ..., 0xff }
- not what you originally had in your String
.
String
has a member function called c_str()
returning "A pointer to the C-style version of the invoking String":
display.drawBitmap(abc.c_str(), startX, startY, bmpWidth, bmpHeight, GxEPD_WHITE);
// ^^^^^^^^
If the compiler complains that c_str()
returns the wrong type for display.drawBitmap()
, you can cast:
display.drawBitmap(reinterpret_cast<const unsigned char*>(abc.c_str()),
startX, startY, bmpWidth, bmpHeight, GxEPD_WHITE);
CodePudding user response:
String abc ="0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff"
There is no String in the default C (or C ) libs. For regular usage consider -
std::string abc = "0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff"
This will not work as you need the byte value 0xff, not the string "0xff"
You also need to consider the difference between char, signed char and unsigned char. All these are 1-byte each, the meaning is different. e.g, 0xFF - when it is treated as signed char, it is negative number; but it is 255 as unsigned char. For char, the representation is compiler-specific.
If you are dealing with binary data, its best to use uint8_t - this is the same as unsigned char. So, for image processing, use uint8_t, uint16_t and uint32_t - this will ensure better portability. Each byte, short and int are guaranteed to be 1-byte, 2-bytes and 4-bytes respectively, and you will avoid bugs due to the sign-bit.
Thus, you can use
const uint8_t displaydata[]={ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};