Jump to content

  • Log In with Google      Sign In   
  • Create Account

Converting from wchar_t to char*


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Perferati   Members   -  Reputation: 138

Like
0Likes
Like

Posted 13 May 2006 - 06:26 AM

Hey all, Had some success earlier tracking down my problem (was using ANSII strings instead of the UNICODE which my program is set to) Now im having trouble converting from wchar_t to char* Thought a simple wcstombs(chPath, wString, sizeof(wString+1)); should work, but gives me a garbled mess...something I am clearly missing here. Could someone please give me some sample code on how to convert wchar_t to char*? Would be much appreciated. Perferati

Sponsor:

#2 TheTroll   Members   -  Reputation: 882

Like
0Likes
Like

Posted 13 May 2006 - 06:28 AM

Why are you doing this? You do realize you are loosing information if you do that.

theTroll

#3 griffin2000   Members   -  Reputation: 214

Like
0Likes
Like

Posted 13 May 2006 - 06:35 AM

I beleive wchar is just unicode-16. So for valid ASCII text each char will be in the form 0x00AB, where 0xAB is a valid 8-bit ASCII char. If it is not in this form (e.g. your first byte !=0) then it does not map trivially to ASCII. This is how my conversion function works (and I have yet to have any problems).

#4 IFooBar   Members   -  Reputation: 906

Like
0Likes
Like

Posted 13 May 2006 - 06:55 AM



wchar_t* wstr = L"A wide character string.";

char* ascii = new char[wcslen(wstr) + 1];

wcstombs( ascii, wstr, wcslen(wstr) );




sizeof(swtr) will return either 4 or 8 depending on the size of a pointer. Because that's what it is - a pointer to wide characters. It's teh equivalunet of doing:

sizeof(wchar_t*);

Which is not what you want, you need the number of characters that your string points to. For that use wcslen for wide chars and strlen for normal chars.



#5 AsOne   Members   -  Reputation: 326

Like
0Likes
Like

Posted 13 May 2006 - 07:14 AM

I'm an idiot, I thought you wanted wchar_t* to char* ... I deleted the rest of my post.

EDIT: This works.

wchar_t wc = 'H';
char c[2];
wctomb(c, wc);
cout << c;



#6 Perferati   Members   -  Reputation: 138

Like
0Likes
Like

Posted 13 May 2006 - 12:43 PM

Thanks for all the help guys!

Perferati




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS