Home > front end >  Ardinio convert string to const unsigned char [closed]
Ardinio convert string to const unsigned char [closed]

Time:09-22

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:

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.

CodePudding user response:

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.

  •  Tags:  
  • c
  • Related