Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Implement WideCharToMultiByte in C# without using WideCharToMultiByte

Status
Not open for further replies.

MuadDubby

Programmer
Sep 23, 1999
236
CA
I'm working on a C# app that needs to convert UTF16 text to UTF8, and WideCharToMultiByte (an unmanaged API in kernel32.dll) does it quite nicely.

Problem is - it's the only unmanaged piece of code in my solution, and I'd give my kingdom (as small as it may be) for advice on how to implement it in C# and therefore remove the dependency on non-dotnet resources.

I've done quite a bit of searching and playing around, and have tried quite a few things with the encoder classes. Nothing gets there.

Here's the encoder code that got the closest:
Code:
string sourceString = "betún";
string destString = "";
System.Text.UTF8Encoding utf8Encoding = new System.Text.UTF8Encoding();
byte[] encodedBytes = utf8Encoding.GetBytes(sourceString);
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
destString = asciiEncoding.GetString(encodedBytes);

The output of this is that destString = "bet??n".

If, however, I try to convert the string with a call to old-faithful as follows:

Code:
string sourceString = "betún";
StringBuilder destStringBuilder = new StringBuilder(myString.Length * 3 + 1);

int result = WideCharToMultiByte(
  65001, //Use the code page used in your unmanaged code 
  0, // Flags could be 0 in most cases see SDK doc. 
  sourceString, // string to convert
  sourceString.Length,
  destStringBuilder,
  destStringBuilder.Capacity,
  IntPtr.Zero,
  IntPtr.Zero);

The output of this is that destStringBuilder = "betún", which is the correct result.

Any ideas?
 
To my opinion, you'd better keep your kingdom and leave it this way (calling unmanaged WideCharToMultiByte).
There is no need to remove dependency on kernel32.dll, because kernel32 is the ultimate base dll of all Win32 programs.
Even if you succeed removing the call to WideCharToMultiByte, your program will still depend on kernel32.dll.
All Windows versions supporting the .NET framework do contain kernel32.dll, you'd have a serious problem if it didn't.

Marcel
 
Thx for the advice, MKuiper. I guess it makes sense that ultimately my work will be carried out by that DLL one way or the other. I guess I'm trying to be a puritan.

It would still be nice to see if anyone has a solution to this, though.

And JurkMonkey - I didn't know .NET runs on linux. Have you tried it? Is there full support for framework 2?
 
There's mostly full support for 1.1 and it's getting really close with 2.0

The only thing you really can't do is make system level calls (PInvoke) or use 3rd party DLLs safely.

Definately fun to play with though. I do a lot of command line programs in linux using C#.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top