473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

MultiByteToWideChar in .NET - Multibyte to Unicode conversion

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.

Nov 22 '05 #1
2 11779
What is wrong with the pure managed solution? It is even better and faster
in 2.0....
--
MichKa [Microsoft]
NLS Collation/Locale/Keyboard Technical Lead
Globalization Infrastructure, Fonts, and Tools
Blog: http://blogs.msdn.com/michkap

This posting is provided "AS IS" with
no warranties, and confers no rights.

<gr****@artgs.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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.
Nov 22 '05 #2
>[DllImport("Kernel32", CharSet = CharSet.Auto)]

There's no point in specifying CharSet.Auto here since there's only
one MultiByteToWideChar function.

[In, MarshalAs(UnmanagedType.LPStr)] String lpMultiByteStr,
This should be a byte[] instead of a String.

If I initialize a C# string with the following bytes: 43, 3A, 5C, 83,
88, 83, 45, 83, 52, 83, 5C, 00
That's your problem, you shouldn't use a string to store byte values.
A string is already Unicode in .NET so the conversion has already
taken place.

Any insights on a way to get MultiByteToWideChar to work, or a better
solution?


Any reason you don't want to use the Encoding classes?
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Robert Diamond | last post by:
Hi ppl, just a quick question... I need to use "MultiByteToWideChar(stuff)" to convert a char to unicode, so that OleLoadPicturePath can get the image files i want, and load it into a HBITMAP,...
5
by: Ma Siva Kumar | last post by:
Running postgresql-7.3.2-3 which came with Red Hat 9.0. Created a database with unicode encoding (in psql) as below: create database leatherlink with encoding='unicode' template=leatherlinkdb;...
18
by: Zygmunt Krynicki | last post by:
Hello I've browsed the FAQ but apparently it lacks any questions concenring wide character strings. I'd like to calculate the length of a multibyte string without converting the whole string. ...
3
by: yazan jab | last post by:
Is it true that Multibyte characters are : char arrays (witch represent a string from the basic characters set). In this case Wide characters are the way for encoding characters from the...
3
by: Weiping | last post by:
Hi, while upgrade to 8.0 (beta3) we got some problem: we have a database which encoding is UNICODE, when we do queries like: select upper('ÖÐÎÄ'); --select some multibyte character, then...
2
by: Billow | last post by:
And how about MultiByte to unicode string?
1
by: miner49er | last post by:
Hi there, Here's my problem, please help - I think i'm going insane :-) I have written a DLL that returns Wide Char Unicode Chinese Strings. I have a 3rd party Graph control (OCX) that...
10
by: Dancefire | last post by:
Hi, everyone, I'm writing a program using wstring(wchar_t) as internal string. The problem is raised when I convert the multibyte char set string with different encoding to wstring(which is...
2
by: George2 | last post by:
Hello everyone, I need to know the wide character (unicode) and multibyte (UTF-8) values of a character string of czech. I personally know nothing about czech. Is the following approach...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.