D
D
Denis2014-11-28 06:16:05
Programming
Denis, 2014-11-28 06:16:05

Why am I getting 2 extra zeros?

I'm trying to display an 8 byte array on the LCD, I expected to get "0xFF0xFF0xFF0xFF0xFF0xFF0xFF0xFF" after executing this program,
char * read_rom(){
unsigned int i;
charch[8];
char ch1[1];
for(i=0;i<=8;i++){
temporary[i]=0xFFFF;//RX();
sprintf(ch1, "0x%X", temporary[i]);
lcd_display(ch1);
}
return temporary;
}
but as a result I get: "0xFF000xFF000xFF000xFF000xFF000xFF000xFF000xFF00"
Explain to the incompetent why?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Armenian Radio, 2014-11-28
@Trans00

Two bugs at once:
Extra loop iteration:
i or should be <8 or <=7
Length modifier not used:
Fix:
sprintf (ch1, "0x%2X", temporary[i]);

V
Vitaly Pukhov, 2014-11-28
@Neuroware

temporary should be like byte[] and temporary[i]=0xFF;

D
DancingOnWater, 2014-11-28
@DancingOnWater

You cram 16 unsigned into an 8 bit signed char and output a signed char

X
xandox, 2014-11-28
@xandox

to Armenian Radio +
ch1 is declared as char ch1[1] - that is, one byte, and you are trying to stuff 4 into it, of course, C will not tell you a word, but then someday you may be very surprised.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question