|
-
December 21st, 2005, 08:33 AM
#1
Conversion Problem In Multi Byte to Wide Char
Hi, I am working on a Unicode Conversion Module and facing this problem..
I have define
typedef basic_string<TCHAR> tstring;
tstring strFirstMsg;
tstring strSecondMsg;
these two string contains the value which I took, from the input RTF String.
these 2 strings some value in a lang. say chinese..
Now I have to calculate its Unicode Equavalant..
For this I used
wchar_t szUnicodeBuffer[200];
strTempUniCode.assign(strFirstMsg);
strTempUniCode.append(strSecondMsg);
int iWriteByte = 0;
iWriteByte = MultiByteToWideChar(nCodePage, MB_COMPOSITE, strTempUniCode,strTempUniCode.length(),szUnicodeBuffer, 100);
It gives error
error C2664: 'MultiByteToWideChar' : cannot convert parameter 3 from 'class std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned s
hort> >' to 'const char *'
after this error Itried like this
iWriteByte = MultiByteToWideChar(nCodePage, MB_COMPOSITE, (const char*)strTempUniCode.c_str(),strTempUniCode.length(),szUnicodeBuffer, 100);
In this case error was removed, but the output was not correct.
I can NOT USE MFC.
can u suggest what I should do...
Regs
Last edited by developerid; December 21st, 2005 at 08:36 AM.
Reason: Title was editited.
-
December 21st, 2005, 08:51 AM
#2
Re: Conversion Problem In Multi Byte to Wide Char
This -
 Originally Posted by developerid
iWriteByte = MultiByteToWideChar(nCodePage, MB_COMPOSITE, strTempUniCode,strTempUniCode.length(),szUnicodeBuffer, 100);
...Can be:
Code:
iWriteByte = MultiByteToWideChar(nCodePage, MB_COMPOSITE, strTempUniCode.c_str (),strTempUniCode.length(),szUnicodeBuffer, 200);
Note that instead of assuming a static array length limit of 200, you can calculate the length of the output string dynamically as the length of the input string + 1 (which it cannot exceed).
Or, send the last parameter as 0, WiedeCharToMultiByte will return the number of (wide-)characters needed to make the conversion - an amount you can use to determine the array size dynamically.
Last edited by Siddhartha; December 21st, 2005 at 09:02 AM.
-
December 21st, 2005, 09:03 AM
#3
Re: Conversion Problem In Multi Byte to Wide Char
Note that the current buffer you are supplying supports 200 Wide Characters and not 100 - the last parameter was wrong.
-
December 21st, 2005, 09:17 AM
#4
Re: Conversion Problem In Multi Byte to Wide Char
MultiByteToWideChar
This function maps a character string to a wide-character (Unicode) string. The character string mapped by this function is not necessarily from a multibyte character set.
int MultiByteToWideChar(
UINT CodePage,
DWORD dwFlags,
LPCSTR lpMultiByteStr,
int cbMultiByte,
LPWSTR lpWideCharStr,
int cchWideChar );
Parameters
CodePage
[in] Specifies the code page to be used to perform the conversion. This parameter can be given the value of any code page that is installed or available in the system. You can also specify one of the following values:
Value Description
CP_ACP ANSI code page
CP_MACCP Not supported
CP_OEMCP OEM code page
CP_SYMBOL Not supported
CP_THREAD_ACP Not supported
dwFlags
[in] Specifies whether to translate to precomposed or composite wide characters (if a composite form exists), whether to use glyph characters in place of control characters, and how to deal with invalid characters. You can specify a combination of the following flag constants:
Value Description
MB_PRECOMPOSED Always use precomposed characters—that is, characters in which a base character and a nonspacing character have a single character value. This is the default translation option. Cannot be used with MB_COMPOSITE.
MB_COMPOSITE Always use composite characters—that is, characters in which a base character and a nonspacing character have different character values. Cannot be used with MB_PRECOMPOSED.
MB_ERR_INVALID_CHARS If the function encounters an invalid input character, it fails and GetLastError returns ERROR_NO_UNICODE_TRANSLATION.
MB_USEGLYPHCHARS Use glyph characters instead of control characters.
A composite character consists of a base character and a nonspacing character, each having different character values. A precomposed character has a single character value for a base/non-spacing character combination. In the character è, the e is the base character and the accent grave mark is the nonspacing character.
The function’s default behavior is to translate to the precomposed form. If a precomposed form does not exist, the function attempts to translate to a composite form.
The flags MB_PRECOMPOSED and MB_COMPOSITE are mutually exclusive. The MB_USEGLYPHCHARS flag and the MB_ERR_INVALID_CHARS can be set regardless of the state of the other flags.
lpMultiByteStr
[in] Pointer to the character string to be converted.
cbMultiByte
[in] Specifies the size in bytes of the string pointed to by the lpMultiByteStr parameter. If this value is –1, the string is assumed to be null terminated and the length is calculated automatically.
lpWideCharStr
[out] Pointer to a buffer that receives the translated string.
cchWideChar
[in] Specifies the size, in wide characters, of the buffer pointed to by the lpWideCharStr parameter. If this value is zero, the function returns the required buffer size, in wide characters, and makes no use of the lpWideCharStr buffer.
If I Helped You, "Rate This Post"
Thanks
Guna
-
December 21st, 2005, 09:28 AM
#5
Re: Conversion Problem In Multi Byte to Wide Char
 Originally Posted by developerid
In this case error was removed, but the output was not correct.
How do you know the output is not correct? Maybe it's correct but you're using a non-unicode function to print it, so it looks wrong.
Also, there's a certain design problem here because you're using tstrings as the input, and unicode strings as the output. If your program is built using multibyte character set, this is fine. But if your program is built using unicode character set, your're doing an unnecessary conversion. Your whole conversion code should be qualified by "#ifndef _UNICODE".
-
December 21st, 2005, 09:41 AM
#6
Re: Conversion Problem In Multi Byte to Wide Char
Hi,
Thanks for the time devoted by u...Still I have the same prob..
can u have a look once again..
I tried this by replacing the fifth parameter to 200, as well as, the second option by taking length of input string +1, but in both the cases output was not there.
what I did is , after calling the function MultiByteToWideChar..I did as follows
tstring strPrefix.assign(_T("&#x"));//For Unicode (as I have to make a html string from input)
tstring strInterMed;
strInterMed.assign(strPrefix);
strInterMed.append(szUnicodeBuffer);
strInterMed.append(_T(";"));
The Variable szUnicodeBuffer must have the correct output.
here are the my inputs for a chineese character
say:
from Input RTF I got \'87\'BF .This is message for a single character.
I parsed it and got the input string as
strFirstMsg contains 87
strSecondMsg contains BF;
Regs
-
December 21st, 2005, 09:45 AM
#7
Re: Conversion Problem In Multi Byte to Wide Char
I came to know that output is not correct, as when I typed a chineese character, it did not displayed properly in the web controller.
Secondly.. earlier the application was not supporting, and I converted it for UNICODE support, that's why I am using tstring.
Regs
-
December 21st, 2005, 09:50 AM
#8
Re: Conversion Problem In Multi Byte to Wide Char
 Originally Posted by developerid
Code:
tstring strInterMed;
strInterMed.assign(strPrefix);
strInterMed.append(szUnicodeBuffer);
strInterMed.append(_T(";"));
Well, therein lies your problem. If your app is compiled in multibyte mode, you're appending a unicode character array to a multi-byte string, which is obviously wrong. If your app is compiled in unicode mode, then your use of MutiByteToWideChar is wrong, because you don't need a conversion, tstrings are already unicode.
-
January 7th, 2006, 02:27 AM
#9
Re: Conversion Problem In Multi Byte to Wide Char
Hi
I am also working on Unicode , I am also encounter such problem,Then i hardcoded the string.Why not use such(following)code for convertint multibyte to 2 byte code.
In 2 byte what happen text is converted as.. ..
T.E.X.T... in this way so just copy the string convert the string in this way,
char TmpValue2[26];
strcpy(TmpValue2,String_);
int tempVal2=lstrlen(String_);
for(int i=0,j=0;i<tempVal2;i++,j+=2)
{
TmpValue2[j]= String_[i];
TmpValue2[j+1]='\0';
}
TmpValue2[j]='\0';
TmpValue2[j+1]='\0';
TmpValue2[j+2]='\0';
memcpy(m_String.GetBuffer(m_String.GetLength()) ,TmpValue2,25);
In this way convert string and memory copy the string and used as Unicode.
If this code is help to u then rate the post.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|