Jump to content
  • Advertisement
Sign in to follow this  
jagguy

adding 2 datatypes in unicode

This topic is 4318 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

how can you add 2 srtrings of unicode TCHAR together to print out in a font obejct of directX, for example TCHAR *fps,testv[20]; int q=8; fps=L"asdasd"; swprintf(testv,L"%s",q); fps=fps + testv; g_font->DrawText(NULL, testv, -1, &test, DT_LEFT|DT_TOP, 0xffffffff);

Share this post


Link to post
Share on other sites
Advertisement
Are you using C or C++? Assuming you use C, to add wide strings together you use wcscat (strcat for char strings). So your code should look like this:

TCHAR *fps,testv[20];
int q = 8;
fps=L"asdasd";
swprintf(testv,L"%s",q);
wcscat(fps,testv);
// Shouldn't you print out fps instead of testv?
g_font->DrawText(NULL, fps, -1, &test,
DT_LEFT|DT_TOP, 0xffffffff);


EDIT: You should remember TCHARs isn't wide string, they can be chars or wchar_ts depending on defines. Your code will fail if TCHAR is char.

Share this post


Link to post
Share on other sites
Quote:
Original post by CTar
Are you using C or C++? Assuming you use C, to add wide strings together you use wcscat (strcat for char strings). So your code should look like this:

TCHAR *fps,testv[20];
int q = 8;
fps=L"asdasd";
swprintf(testv,L"%s",q);
wcscat(fps,testv);
// Shouldn't you print out fps instead of testv?
g_font->DrawText(NULL, fps, -1, &test,
DT_LEFT|DT_TOP, 0xffffffff);


EDIT: You should remember TCHARs isn't wide string, they can be chars or wchar_ts depending on defines. Your code will fail if TCHAR is char.


Using c++ but I just want the simplest way to do it.
I am using unicode so TCHAR defaults to cater for this I believe.

Share this post


Link to post
Share on other sites
can't add 2 TCHAR* together but can do arrays.


TCHAR *fps,testv[20],*fp,testvv[20];

fp=L"iii";
fps=L"asdasd";

swprintf(testvv,L"%s",fps );

g_font->DrawText(NULL, testvv, -1, &test,
DT_LEFT|DT_TOP, 0xffffffff);

swprintf(testv,L"%d",offsetX);
wcscat(testv,testvv);
wcscat(fp,fps);//access violation

Share this post


Link to post
Share on other sites
Because the memory where the string would be concatenated (not "added") doesn't necessarily belong to you. An array is an actual reserved chunk of memory. A pointer merely says "there's something over there". All wcscat() is doing, essentially, is copying data blindly from one location (starting at beginning of second string, for the length of second string) to another (end of first string).

Count yourself lucky it crashed: it could instead overwrite some other variable, causing something much harder to figure out.

In C++, you should be using the std::string to represent ordinary strings. As it happens, there is also std::wstring for unicode strings, with which you can just use the operator+, nice and clean. You can set up a define similar to how TCHAR works, in order to keep the ability to switch UNICODE on and off:


#ifdef UNICODE
typedef std::wstring TSTRING;
#else
typedef std::string TSTRING;
#endif

TSTRING fp(L"iii");
TSTRING fps(L"asdasd");
TSTRING fpfps = fp + fps;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!