is this conversion from a char buf to a wchar correct?
-
hi, i want to ask a simple question related to my code here. I am using a MessageBox to show the user the estimated time of the battery. The start value is an ULONG which i put into my function as float seconds. The cond param is the returnstring of my function. Is the conversion (because the wsprintf function does not understand the %f param for formatting) right ? And what could i improve ? Thanks !
void gettimestring(float seconds,wchar_t *input)
{float minutes,hours; hours = floorf(seconds/3600); seconds -= hours\*3600; minutes = floorf(seconds/60); seconds -= minutes\*60; char buf\[20\]; sprintf(buf,"%02.0f:%02.0f:%02.0f",hours,minutes,seconds); wsprintf(input,L"%S",buf);
}
bye, gabbana
-
hi, i want to ask a simple question related to my code here. I am using a MessageBox to show the user the estimated time of the battery. The start value is an ULONG which i put into my function as float seconds. The cond param is the returnstring of my function. Is the conversion (because the wsprintf function does not understand the %f param for formatting) right ? And what could i improve ? Thanks !
void gettimestring(float seconds,wchar_t *input)
{float minutes,hours; hours = floorf(seconds/3600); seconds -= hours\*3600; minutes = floorf(seconds/60); seconds -= minutes\*60; char buf\[20\]; sprintf(buf,"%02.0f:%02.0f:%02.0f",hours,minutes,seconds); wsprintf(input,L"%S",buf);
}
bye, gabbana
Try Win32 API "MultiByteToWideChar" For example
int nSize = MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,buf,strlen(buf),input,sizeof(input));
input[nSize] = 0; -
hi, i want to ask a simple question related to my code here. I am using a MessageBox to show the user the estimated time of the battery. The start value is an ULONG which i put into my function as float seconds. The cond param is the returnstring of my function. Is the conversion (because the wsprintf function does not understand the %f param for formatting) right ? And what could i improve ? Thanks !
void gettimestring(float seconds,wchar_t *input)
{float minutes,hours; hours = floorf(seconds/3600); seconds -= hours\*3600; minutes = floorf(seconds/60); seconds -= minutes\*60; char buf\[20\]; sprintf(buf,"%02.0f:%02.0f:%02.0f",hours,minutes,seconds); wsprintf(input,L"%S",buf);
}
bye, gabbana
Why are you using
float
s for such a thing?If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Why are you using
float
s for such a thing?If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Try Win32 API "MultiByteToWideChar" For example
int nSize = MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,buf,strlen(buf),input,sizeof(input));
input[nSize] = 0; -
The calcucaltion of the hours have decimal numbers. So I think i need floats at minimum or what do you mean exactly ?
I mean, for instance
7305 seconds = 2 hours, 1 minute, 42 seconds
for such a conversion floats are not needed:
int iTotSecs = 7305;
int iHours, iMins, iSecs;
iHours = iTotSecs / 3600;
iMins = (iTotSecs % 3600) / 60;
iSecs = iTotSecs % 60;:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I mean, for instance
7305 seconds = 2 hours, 1 minute, 42 seconds
for such a conversion floats are not needed:
int iTotSecs = 7305;
int iHours, iMins, iSecs;
iHours = iTotSecs / 3600;
iMins = (iTotSecs % 3600) / 60;
iSecs = iTotSecs % 60;:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
The calcucaltion of the hours have decimal numbers. So I think i need floats at minimum or what do you mean exactly ?
gabbana wrote:
The calcucaltion of the hours have decimal numbers.
So then use a
double
."Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
ohh yes that's right, but if i could remember the compiler put out some warnings and this was it i want to avoid.
To eliminate all the warnings a simple cast on the original variable is enough. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
hi, i want to ask a simple question related to my code here. I am using a MessageBox to show the user the estimated time of the battery. The start value is an ULONG which i put into my function as float seconds. The cond param is the returnstring of my function. Is the conversion (because the wsprintf function does not understand the %f param for formatting) right ? And what could i improve ? Thanks !
void gettimestring(float seconds,wchar_t *input)
{float minutes,hours; hours = floorf(seconds/3600); seconds -= hours\*3600; minutes = floorf(seconds/60); seconds -= minutes\*60; char buf\[20\]; sprintf(buf,"%02.0f:%02.0f:%02.0f",hours,minutes,seconds); wsprintf(input,L"%S",buf);
}
bye, gabbana
gabbana wrote:
char buf[20]; sprintf(buf,"%02.0f:%02.0f:%02.0f",hours,minutes,seconds); wsprintf(input,L"%S",buf);
Instead of using char memory to variable buf, why don't you simply allocate the memory to wchar!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/xml>