0

I'm trying to display a bitmap on gLCD 128x64, I stored the bitmap into txt file 1.txt in a SD card this is a part of bit map as following:

0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00.......

I want to convert the content of this text into array: Unsigned char logo[] with making a split on ",".

Any help please

Greenonline
3,1527 gold badges36 silver badges48 bronze badges
asked Apr 22, 2019 at 9:12

1 Answer 1

1

The most efficient way is to read the file byte-by-byte into a small character array until you get a comma or the end of file - ignoring any characters that aren't of interest (line feeds, etc).

Then for each chunk that you read you then convert it to an integer.

For example (untested):

char temp[5] = {0}; // should be more than enough
int pos = 0;
int bno = 0;
const char *hexchars = "0123456789abcdefABCDEFx";
while ((int ch = myFile.read()) >= 0) {
 if (ch == ',') { // We found a comma
 logo[bno++] = strtoul(temp, NULL, 16); // Base 16 conversion
 pos = 0;
 temp[0] = 0;
 } else if (strchr(hexchars, ch) != NULL) {
 if (pos < 4) {
 temp[pos++] = ch;
 temp[pos] = 0;
 }
 }
}

Of course, you have to know how big the logo array has to be beforehand.

One does, though, have to ask "Why are you storing a graphical logo as a textual representation of binary data" in the first place? It would be far easier to just store it as a simple binary file and read the data directly into the logo array with a single File::read(void *buf, size_t nbyte); function call. Using text as an intermediate format is both wasteful and needlessly increases complexity massively.

answered Apr 22, 2019 at 10:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.