PDA

View Full Version : converting ASCII to Unicode


ianlarner
22nd November 2005, 08:10 PM
hi ther i wonder if you could help me on this simple task. I'm creating a GPS application to run on the XDA2, i'm using eVC++ to do the implementation.

at the moment i'm reading the GPS signal via bluetooth over a virtual COM port, the signal coming from the GPS if a ASCII sinal and i'm duimping this into a char buffer.

However i need to convert this to UNICODE in order to display it on the Pocket PC, how's best to convert a buffer full of ASCII into Unicode so i may display it?

I tried using MultiByteToWideChar(), but it doesn't seem to work properly, maybe i haven't set it up correctly? Could someone point me in the right direction!

Below is an example of what i tried:


char buf[50]; // contains output from GPS
TCHAR Message[50]; //where i intended to put the message so i could display it

MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, buf, -1, Message, 0);



Thank in advance

vijay555
22nd November 2005, 09:05 PM
I'm sure it's not the right way to go about it, but I generally wsprintf for short strings.

However, don't listen to me, I'm a mad man. Check this page out instead:
http://www.i18nguy.com/unicode/c-unicode.html

V

ianlarner
23rd November 2005, 08:11 PM
Thanks for that, out of curiosity, how would you use wsprintf to convert ASCII to unicode, i tried that before with no real success!

vangelderp
23rd November 2005, 10:01 PM
The last value passed to MultiByteToWideChar tells this function the size of the result buffer, Message in your case. You have passed zero, all that does is makes the function return the size of a TCHAR variable it needs to put the Ascii input buf into.

You need to put sizeof(Message) as the last parameter and not zero.

The other way (better way ?) of doing this is first you call the MultiByteToWideChar function with the zero parameter as you have and then you malloc the result * sizeof(TCHAR).

ianlarner
23rd November 2005, 11:04 PM
Thanks for the advise, after looking into the function more i realised this is where i was going wrong, and i have now managed to make the conversion. Thanks for pointing that out though!