I have data stored in text_buf[4] = {0x1d, 0x72, 0x2f, 0x32}
.When I send it to display on LCD, it displays some garbage. I understand that LCD displays only string, how do I make it to display hex values that is stored in text_buf
? Are there any C examples for this? Please do let me know.
Asked
Active
Viewed 1,936 times
0

Grijesh Chauhan
- 57,103
- 20
- 141
- 208

user2874122
- 3
- 2
-
1Possible duplicate? http://stackoverflow.com/questions/3649026/how-to-display-hexadecimal-numbers-in-c – Rivasa Oct 12 '13 at 13:57
-
1do you have any code? what is the desired output? if your LCD prints only strings - convert the buffer to a string and print it – jev Oct 12 '13 at 14:02
2 Answers
6
Use snprintf()
family to format your text_buf
(or any data) to a char
array and call your LCD's display routine.
char line[256];
snprintf(line, sizeof(line), "%02x %02x %02x %02x\n", text_buf[0], text_buf[1], text_buf[2], text_buf[3]);
LCD_print(line); // LCD's display routine

leesei
- 6,020
- 2
- 29
- 51
-
3You "misspelled" `snprintf()`. Also, no cplusplus.com, please. Firstly because it's bad, secondly because this is a C question. – Oct 12 '13 at 14:15
-
Ha, I'll "correct the spelling" then. But that reference is to the C library and so I think it is suitable. – leesei Oct 12 '13 at 14:18
-
2unfortunately, that's not the case. I suggest you take a look at [a true C reference](http://pubs.opengroup.org/onlinepubs/7908799/xsh/sprintf.html). – Oct 12 '13 at 14:19
-
-
@MatteoItalia It is. But at least it's official and it doesn't suffer from hundreds of fundamental errors. And it's about C, not C++. – Oct 12 '13 at 14:25
-
I intended to provide an easy reference of formatting sequence to the OP (who seems to be new to C). The official things are not user friendly IMHO. – leesei Oct 12 '13 at 14:28
-
1Using C++ documentation for the C stdlib is not `programmer friendly` , nor is it `computer friendly` . – wildplasser Oct 12 '13 at 14:30
-
@leesei Why do you think that a C++ reference is more "user friendly" than a C one? (Also, as I just stated, it's wrong. Would you rather supply your user with a wrong but "friendly" reference than a correct but more technical one? I hope not.) – Oct 12 '13 at 14:32
-
-
Do not take this as a challenge, but I genuinely are not aware of the difference of the `printf()` API/implementation difference in C and C++. I only meant the tabulated layout and examples are more user friendly for a learner. – leesei Oct 12 '13 at 14:35
-
@leesei No problem. There's no difference (well, there should be no difference...) between `printf()` in `
` and `printf()` in ` – Oct 12 '13 at 14:38`. It's only that one should not contribute to the common misconception that "C is a subset of C++", since it isn't. I (and apparently others too) felt that you kind of supported this by treating C and C++ things as if they were interchangeable. I see that you wanted to do something good :) This is just a piece of advice. -
@H2CO3 K, but C tutorial are indeed hard for beginners (they are all ASCII text ;-p). Do you guys have any recommendations beyond SUS? – leesei Oct 12 '13 at 14:42
-
@leesei, for learning C? K&R's 2nd Ed. is a good book as well Kochan's `Programming in C` – Rivasa Oct 12 '13 at 14:44
-
Any other online reference? (I'll keep SUS in my bookmark from now on.) – leesei Oct 12 '13 at 14:45
3
Be safe and use snprintf()
. Also add a loop to generalize the task and to decrease redundancy:
char text[0x1000]; // or however many
size_t n = 0;
for (size_t i = 0; i < sizeof(buf) / sizeof(buf[0]); i++) {
int delta = snprintf(text + n, sizeof(text) - n, "%02x ", buf[i]);
if (delta < 0)
break; // handle error!
n += delta;
}
Technically, the documentation above concerns POSIX, not only standard C. So in case anyone wonders: section 7.19.6.5 of the C99 Standard (I couldn't find the C11 one online) concerns this very function. Clause 7.19.6.1.8 describes the conversion specifiers.
-
2I originally down voted this answer because it is overly complex in my opinion. H2C03 and I had a long "discussion" and in the end he pointed out to me that down votes are for technically incorrect answers or spam. I am removing my down vote because his answer is not incorrect nor spam.. – Charlie Burns Oct 12 '13 at 15:17