PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Probem with swprintf on x86

Maybe it's just late, but...

I'm working on a GapiDraw game, but I have a general question... Why is it that the following:

Code:

int countdown_seconds = 30;
TCHAR outStr[10] = TEXT("");
swprintf((unsigned short*)&outStr;, L"%d", countdown_seconds);

...results in outStr = "3" instead of "30"? In other words, I'm only getting the first digit into outStr no matter what I do. This is on the desktop, I haven't compiled for PocketPC yet.

Thanks all!
Argh, the following works:

Code:

char outStr[10] = "";
sprintf(outStr, "%d", countdown_seconds);


Does this mean I have to wrap blocks like this in proprocessor checks and do TCHAR's for PocketPC and straight char's on the desktop? Or is it possible to do it so that it will compile and work on both targets? Anyone?
Use _stprintf.
For desktop builds it is non-unicode by default. The T types are either WideChar or ANSI depending on if you are building unicode or ansi (whether UNICODE is defined). That is why you are having to manually cast outStr to unsigned short to build, because that TCHAR array is really being resolved as chars. So use WCHAR instead. Also TEXT and T are dependant on the build type, so use L instead. However, you don't need to initialize your arrays for sprintf, strcpy, etc, because they overwrite the array.

So what is happening is you are filling an array of chars with:

'3' 0 '0' 0 0 0

So when you view the ANSI string in the debugger it sees the 3 and then a null, giving you a string of just "3".

Make sure you don't mix and match the 3 types (char, TCHAR, WCHAR) or functions. For actual Pocket PC builds you can get by swapping TCHAR and WCHAR because TCHAR is defined as WCHAR in that case, but you run into problems with regular ANSI builds. So don't do that. :)

Dan East
Anonymous wrote:
Use _stprintf.


For a non-unicode build that would resolve to sprintf, which I don't think is his intention.

DAn East
Maybe I misread his intention then. I was under the impression that he wanted to write portable code between the PC and the PocketPC so that he wouldn't have to have separate code for each and surround the code in #ifdefs. Using TCHAR arrays and the "tchar" version of the library routines (such as _stprintf) will do this and they are designed exactly for this purpose.

TCHAR outStr[32];

_stprintf(outStr, TEXT("%d"), countdown_seconds);


That code will work on the PocketPC and the PC. No muss - no fuss. That is what I thought he was asking how to do.

Sometimes I find the most difficult part of providing help to people on this site is actually understanding what they are trying to do.
Anonymous wrote:
Sometimes I find the most difficult part of providing help to people on this site is actually understanding what they are trying to do.


You will find that this is a "problem" anywhere, and not only on web sites. Problem solving often has a communication problem at his roots.
Anonymous was correct in what I was asking...

What I coded last night worked and was simply this:

TCHAR outStr[4];
#ifdef _WIN32_WCE
swprintf(outStr, L"%d", countdown_seconds);
#else
sprintf(outStr, "%d", countdown_seconds);
#endif

I wanted to be able to code it so that I didn't need the #ifdef's. His answer was precisely correct, I just tried it, and was precisely what I was looking for.

As for communication issues, try getting eight months into developing a major project and find out that someone didn't communicate the specs properly for one of the primary screens of the system. That has happened to me, and that's not unique in the industry. If someone misunderstands me here, big f'ing deal, no harm no foul. Other places though, it's, shall we say, somewhat worse?!?
You get specs? Analysis?

<Faint>

:twisted:
Yeah, I do. Well, let me be precise: I get a BRD and a half-asses FRD, and then I get a number of the lead BA to call when I have questions.

I don't suspect I'd recognize REAL specs if I saw them. Then again, I've moved into more of the architect role anyway, so I'm usually the one writing something that resembles specs these days (or playing architect as well as lead (read: SOLE) developer).