Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with sprintf formatting ( I think)..

Status
Not open for further replies.

deepsheep

Programmer
Sep 13, 2002
154
CA
I am enhancing a program that runs on a piece of hardware designed by my company. I don't do a lot of C programming. To make a long story short, I can't print a value correctly.

My current code is:
sprintf(out, "%x\n", myStuct.lastPrinted[1]);

myStruct.lastPrinted is an array of byte. It is holding a date-time that should be properly displayed when printed in hex form.
When it is printed (one position at a time), I get this :

307 //year
707 //month
807 //day
1307 //hour
5207 //minute
3007 //second

This date would be July 8, 2003 13:52:30.
The "07" part varies with the date, but I've only seen "0"-something.

If I print it out like this:

sprintf( tempString, "%x/%x/%x %x:%x:%x\n", myStuct.lastPrinted[0],myStuct.lastPrinted[1],
myStuct.lastPrinted[2],myStuct.lastPrinted[3],
myStuct.lastPrinted[4],myStuct.lastPrinted[5]);

I would get: 307/813/5230 ff:ff:232

My suspicion is that it's pulling two bytes instead of just the one byte the value is stored in. I've been having a tough time trying to prove this, so I thought I'd ask.

What's the best way to solve this problem?
 
Looks like it is taking the bytes after the data you have specified. Try this
Code:
 sprintf(out, "%x\n",  (int)myStuct.lastPrinted[1]);
 
That seems to do it. I'm not sure why I didn't think of it, too much VB and Java I think.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top