GDI TextOut Problem
I want to print the value of a variable, but if var=5 TextOut print 5, and i want to be printed 05, how can i do it?
char s[2];
itoa(time.wSecond,&s,10);
TextOut(hDC,0,0,s,strlen(s));
// out
s[0] = "2"
s[1] = "1"
out: 21
but if i want to put 01, it print only 1
thnx
Try using sprintf instead of itoa. You might have to enlarge the buffer a little to account for the terminator
sprintf(str,"%02d",time.wSecond);
"%02d" - the 02 before the type specifier indicatates that numbers less than 10 should have a zero prepended to them - that''s the effective outcome at any rate. Read the docs on printf format specifiers to get the proper explaination.
sprintf(str,"%02d",time.wSecond);
"%02d" - the 02 before the type specifier indicatates that numbers less than 10 should have a zero prepended to them - that''s the effective outcome at any rate. Read the docs on printf format specifiers to get the proper explaination.
what means the parameter str?, sprintf can be used to print in a window or in a device context?
sprintf puts something into a string, not print something on the screen
PSTR str;
int mynumber;
mynumber=2;
wsprintf(str,"%02d",mynumber);
TextOut(bdc,100,100,str,strlen(str)-1);
--Fireking
Owner/Leader
Genetics 3rd Dimension Development
PSTR str;
int mynumber;
mynumber=2;
wsprintf(str,"%02d",mynumber);
TextOut(bdc,100,100,str,strlen(str)-1);
--Fireking
Owner/Leader
Genetics 3rd Dimension Development
Fireking's example doesn't allocate any memory for str - so there's no buffer there and thus you get the error. The char array that you initially began with should do the trick
char str[4]; // one extra byte for the terminator, one extra for padding
sprintf(str,"%02d",mynumber);
and so forth as fireking showed.
// edit:
in the format specifier - 02 - the zero indicates that leading spaces should be filled with 0 and the 2 indicates the minimum size to use. So if mynumber requires more than 2 digits those will get printed as well.
[edited by - lessbread on September 9, 2002 6:30:02 PM]
char str[4]; // one extra byte for the terminator, one extra for padding
sprintf(str,"%02d",mynumber);
and so forth as fireking showed.
// edit:
in the format specifier - 02 - the zero indicates that leading spaces should be filled with 0 and the 2 indicates the minimum size to use. So if mynumber requires more than 2 digits those will get printed as well.
[edited by - lessbread on September 9, 2002 6:30:02 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement