char* to wchar_t Convert 
Author Message
 char* to wchar_t Convert

char    str[3] = "ko";

wchar_t test[2] = L'str';

test is L"s" I want test L"ko"
How To Convertion??

Thank!



Wed, 09 Feb 2005 01:07:03 GMT  
 char* to wchar_t Convert
Huh?  You'll have to restate the question more clearly.

Paul T.


Quote:
> char    str[3] = "ko";

> wchar_t test[2] = L'str';

> test is L"s" I want test L"ko"
> How To Convertion??

> Thank!



Wed, 09 Feb 2005 01:19:23 GMT  
 char* to wchar_t Convert

Quote:

> char    str[3] = "ko";

> wchar_t test[2] = L'str';

> test is L"s" I want test L"ko"
> How To Convertion??

Use mbstowcs.

--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)



Wed, 09 Feb 2005 01:26:38 GMT  
 char* to wchar_t Convert
Thanks.

Used str[3] Converting is test.

Want test == L"ko" value states

plz.


Quote:
> Huh?  You'll have to restate the question more clearly.

> Paul T.



> > char    str[3] = "ko";

> > wchar_t test[2] = L'str';

> > test is L"s" I want test L"ko"
> > How To Convertion??

> > Thank!



Wed, 09 Feb 2005 01:33:32 GMT  
 char* to wchar_t Convert
Thanks Pete Becker!

Used function!

Quote:

> > char    str[3] = "ko";

> > wchar_t test[2] = L'str';

> > test is L"s" I want test L"ko"
> > How To Convertion??

> Use mbstowcs.

> --
> Pete Becker
> Dinkumware, Ltd. (http://www.dinkumware.com)



Wed, 09 Feb 2005 02:11:54 GMT  
 char* to wchar_t Convert
Hi,
 To convert regular ( single byte) string to wide character
string(multibyte),
use mbstowcs( ) function. ( which stands for  Multi Byte String TO Wide
Character String ).

When u use macro L along with 'str', viz L'str', the compiler treats 'str'
as a single character because u used single quotes
and not double quotes. Also the macro L will tell the compiler to treat the
single character as unsigned short integer.
So your result is single character which is two bytes long.
Hence use function mbstowcs() and wcstombs( ) for the reverse.

U can als write your own function like below
wchar_t* mbsToWcs( char * str)
{
    unsigned short counter, nLen = strlen(str);
    wchar_t *test = new wchar_t[ nLen + 1];
    for( counter=0; counter< nLen; counter++) {
         test[counter] = str[counter];
    }
    test[counter] = '\0' ;
    return test;

Quote:
}

int main()
{
     char   str[3] = "ko";
    wchar_t test = mbsToWcs( str);

Quote:
}

-Chandresh


Quote:
> char    str[3] = "ko";

> wchar_t test[2] = L'str';

> test is L"s" I want test L"ko"
> How To Convertion??

> Thank!



Wed, 09 Feb 2005 15:17:03 GMT  
 char* to wchar_t Convert

Quote:

> Also the macro L will tell the compiler to treat the
> single character as unsigned short integer.

L is not a macro. It is the first character of a wide character literal.
It tells the compiler to translate the characters that follow to a
single wchar_t (if the characters are enclosed by single quotes) or to
an array of wchar_t (if they're in double quotes). In C wchar_t is a
typedef for some integral type. In C++ it is a builtin type with the
same representation as one of the other integral types. In Microsoft
compilers prior to 7.0 (okay, VC++.NET) wchar_t is a typedef in C++ as
well as in C, and it's a typedef for unsigned short. In C++ with 7.0 and
above it is a distinct type, as the language definition requires.

Unsigned short has turned out to be short-sighted. Unicode now has
nearly 100,000 code points, so a 16-bit unsigned value can't handle all
of Unicode. (Note that UTF-16 isn't a valid wide character
representation, because the width of characters in UTF-16 isn't fixed)

--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)



Wed, 09 Feb 2005 21:15:42 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Converting char into wchar_t

2. Converting char into wchar_t

3. convert char to wchar_t

4. convert wchar_t* to char*

5. Need to convert wchar_t to char *

6. Converting char * to wchar_t *

7. char * to wchar_t (char string to wide string)

8. please help... converting char to string to wchar_t, or manipulating wchar_t directly

9. Converting char array of literals chars to escape chars

10. char to wchar_t promotion

11. wchar_t vs. char

12. wchar_t* to char* conversions

 

 
Powered by phpBB® Forum Software