G
groups
I have a C# application which needs to convert MultiByte strings to
Unicode.
However, I cannot get MultiByteToWideChar to behave as expected within
..net.
I have declared it as follows:
[DllImport("Kernel32", CharSet = CharSet.Auto)]
static extern Int32 MultiByteToWideChar(
UInt32 codePage,
UInt32 dwFlags,
[In, MarshalAs(UnmanagedType.LPStr)] String lpMultiByteStr,
Int32 cbMultiByte,
[Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpWideCharStr,
Int32 cchWideChar);
And am using it as follows:
private string ConvertToUnicode( string str, uint codepage)
{
int l = str.Length;
int i = 0;
i = MultiByteToWideChar( codepage, 0, str, -1, null, 0);
StringBuilder wideStr = new StringBuilder(i);
i = MultiByteToWideChar( codepage, 0, str, -1, wideStr,
wideStr.Capacity);
string s = wideStr.ToString();
return s;
}
If I initialize a C# string with the following bytes: 43, 3A, 5C, 83,
88, 83, 45, 83, 52, 83, 5C, 00 and use the ConvertToUnicode function
above with codepage 932 (Japanese), i get garbage (C:\???E?R?\).
However, using a pure .NET solution (below) I get the correct string
(C:\ヨウコソ):
private string MultibyteToUnicodeNETOnly( string str, int codepage)
{
byte[] source = MCBSToByte(str);
Encoding e1 = Encoding.GetEncoding(codepage);
Encoding e2 = Encoding.Unicode;
byte[] target = Encoding.Convert( e1, e2, source);
return e2.GetString( target);
}
private byte[] MCBSToByte(string s)
{
byte[] b = new byte[s.Length];
int i = 0 ;
foreach( char c in s)
b[ i++] = (byte)c;
return b;
}
Any insights on a way to get MultiByteToWideChar to work, or a better
solution? Thanks in advance.
Unicode.
However, I cannot get MultiByteToWideChar to behave as expected within
..net.
I have declared it as follows:
[DllImport("Kernel32", CharSet = CharSet.Auto)]
static extern Int32 MultiByteToWideChar(
UInt32 codePage,
UInt32 dwFlags,
[In, MarshalAs(UnmanagedType.LPStr)] String lpMultiByteStr,
Int32 cbMultiByte,
[Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpWideCharStr,
Int32 cchWideChar);
And am using it as follows:
private string ConvertToUnicode( string str, uint codepage)
{
int l = str.Length;
int i = 0;
i = MultiByteToWideChar( codepage, 0, str, -1, null, 0);
StringBuilder wideStr = new StringBuilder(i);
i = MultiByteToWideChar( codepage, 0, str, -1, wideStr,
wideStr.Capacity);
string s = wideStr.ToString();
return s;
}
If I initialize a C# string with the following bytes: 43, 3A, 5C, 83,
88, 83, 45, 83, 52, 83, 5C, 00 and use the ConvertToUnicode function
above with codepage 932 (Japanese), i get garbage (C:\???E?R?\).
However, using a pure .NET solution (below) I get the correct string
(C:\ヨウコソ):
private string MultibyteToUnicodeNETOnly( string str, int codepage)
{
byte[] source = MCBSToByte(str);
Encoding e1 = Encoding.GetEncoding(codepage);
Encoding e2 = Encoding.Unicode;
byte[] target = Encoding.Convert( e1, e2, source);
return e2.GetString( target);
}
private byte[] MCBSToByte(string s)
{
byte[] b = new byte[s.Length];
int i = 0 ;
foreach( char c in s)
b[ i++] = (byte)c;
return b;
}
Any insights on a way to get MultiByteToWideChar to work, or a better
solution? Thanks in advance.