0
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

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
Coder06
  • 11
  • 3
  • your first 0x00 terminates this as a C string, but it's not composed of printable characters anyway. I'm not sure what you're asking. An `unsigned char` will only hold _one_ of the bytes in your array. If you want to copy this data to another array, use [`memcpy`](https://man7.org/linux/man-pages/man3/memcpy.3.html). Do not use strings functions, this is not string data. – yano Sep 21 '21 at 17:57
  • based on the extra info in your answer (which should go into the question as mention), you're saying the server is returning an actual C string to you, `"0xff, 0xff, 0xff ..."`, and not simply a byte array `{0xff, 0xff, 0xff ...}`? That's silly, talk to the author of the server. But until then, you can use a combo of [`strtok`](https://man7.org/linux/man-pages/man3/strtok_r.3.html) and [`strtol`](https://man7.org/linux/man-pages/man3/strtol.3.html) to convert byte-by-byte to fill an `unsigned char` array. – yano Sep 21 '21 at 19:21
  • What exactly is the data type `String`? Is that [pseudo-code](https://en.wikipedia.org/wiki/Pseudocode) or actual C code? When you write `String abc ="0xff, 0xff, [...]"`, do you actually mean `char abc[] = "0xff, 0xff, [...]"`? – Andreas Wenzel Sep 21 '21 at 19:52

2 Answers2

1

If you are trying to parse a string 0xff, 0xff, ..., 0xff, 0xff to produce an array of unsigned char, then you have a bit of code to write.

But I suspect you simply want to type those characters in a manner that declares them to be a constant array of unsigned char.

To do the latter, you simply type this:

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

Mostly it's about replacing your " with { and } after using the proper datatype on the left-hand side of the =.

If you do indeed need to visually display the array as a string and you need to process that string as an array of unsigned char, then you need both representations but then you should consider which conversion is easier and less prone to error.

If we assume that your code rarely needs to display the string compared to the often processing of the array, then you should consider how much easier and less error prone it is to produce the string than it is to parse the string to produce the array.

Jeff Holt
  • 2,940
  • 3
  • 22
  • 29
0

Define your data as a raw string literal.

Essentially if you have a string "your string" define it as std::string str = R"(your string)";

A raw string literal is a string in which the escape characters (like \n \t or " ) of C++ are not processed. A raw string literal which starts with R"( and ends in )" was introduced in C++11.

moi
  • 467
  • 4
  • 19
  • tring 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 ? – Coder06 Sep 21 '21 at 18:27