Skip to main content
gamedev.net
🔒 Locked

Need help about UNICODE

Started by Mescalito 5 replies 500+ views
Original Post
Mescalito
Mescalito
Hi everyone, i didn''t knwo where to post a question about UNICODE format so I posted there... So here''s my problem: I need to developp a application using COM interfaces and these are currently using an UNICODE string format (used to give filenames for examples); this format uses 2 bytes for a character (standard string are [char] coded so it''s only 1 byte). I have so troubles using this format as i use standard C functions to managed all my strings (strcpy, strcmp.. aand all str___() functs, fopen, fclose, fprintf, fwrite and fread for files...). How can we "convert" a simple [char*] string to a [unsigned short*] one (the unicode string format) WITHOUT LOSS OF VALIDTY I have tried a (barbarian ) convertion (with casting) but he didn''t keep the filename valid! So can anyone help me about that !!! Thanks for any reply
dalleboy
dalleboy
The following example will allocate memory on the stack, if you don''t like that you should replace alloca.

  

#include <malloc.h>

const char* sz = "narrow string";
const size_t uiLength = strlen(sz) + 1;
wchar_t* wsz = (wchar_t*) alloca(uiLength * 2);
MultiByteToWideChar(CP_ACP, 0, sz, -1, wsz, uiLength);
// Here you could use your wsz, when it falls out of scope it will be deallocated.


You could hardcode it like this if you need to:

  
const wchar_t* wsz = L"constant wide string";
Fruny
Fruny
First, the wide-character type isn''t unsigned short, but wchar_t. Even though VC6 treats it as a typedef, it really is a basic type, just as much as char, int, short ...

If you''re intent on using C functions, there are wide-character versions available, usually by adding a ''w'' in front of the function name. Whether the encoding is Unicode or something else depends on your locale. I strongly encourage you to read up on locales !

Alternatively, use the C++ wide string type (std::wstring) to store the Unicode text. Assigning to one such should automatically convert from char to wchar_t, using the std::ios::widen function (which is, once again, dependent on locales, read up on them).

Finally, one last option is to download the Boost library and to make use of their lexical_cast function (which uses stringstreams to do the conversion)

Using it, the code ought to look like (assuming the appropriate using statements have been issued) :


MyCApiFunction( lexical_cast<wstring>( mystring ).c_str() );


This ought to work regardless of whether mystring is a C-style NULL-terminated narrow string or a C++ narrow string object.

If not, that should at least have given you some starting points.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Mescalito
Mescalito
Has unicode strings have an "end" character ???

(every string (char * type) have an ''\0'' char at the end of it)
Fruny
Fruny
quote:
Original post by Mescalito
Has unicode strings have an "end" character ???

(every string (char * type) have an ''\0'' char at the end of it)


Not every string has a ''\0'' at the end, only C-style (a.k.a. ''null-terminated'') strings do. Other schemes involve keeping track of the total string length. That''s how strings work in Pascal, VB, C++ ...

That being said, a null-terminated wide string would obviously end up with a null wide-character...


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
dalleboy
dalleboy
quote:
Original post by Mescalito
Has unicode strings have an "end" character ???

(every string (char * type) have an ''\0'' char at the end of it)

A zero terminated wide string will have a zero at the end (or rather a L''\0'').



Update GameDev.net system time campaign - success at last

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.