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
1 Answer 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.
Explore related questions
See similar questions with these tags.