Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Internationalization / October 2004

Tip: Looking for answers? Try searching our database.

Marshal.StringToHGlobalAnsi and multibyte strings

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hesse - 19 Oct 2004 20:47 GMT
Does Marshal.StringToHGlobalAnsi convert System.String to multibyte strings
if necessary (if the System.String contains non-ANSI characters), or does it
only convert ansi strings?

If it converts UNICODE characters to appropriate multibyte characters, what
code page does it use for the conversion? Is it the operating system
(Windows) code page (i.e. on Japanese windows, will it use the Japanese code
page)?

Thanks!
Jochen Kalmbach - 19 Oct 2004 21:00 GMT
=?Utf-8?B?SGVzc2U=?= wrote:

> Does Marshal.StringToHGlobalAnsi convert System.String to multibyte
> strings if necessary (if the System.String contains non-ANSI
> characters), or does it only convert ansi strings?

It converts to MBCS.

See: What is an ANSI string?
http://blog.kalmbachnet.de/?postid=19

> If it converts UNICODE characters to appropriate multibyte characters,
> what code page does it use for the conversion? Is it the operating
> system (Windows) code page (i.e. on Japanese windows, will it use the
> Japanese code page)?

I think they use "CP_ACP" (but I am not sure.. maybe I will dig into it...)

Signature

Greetings
 Jochen

  My blog about Win32 and .NET
  http://blog.kalmbachnet.de/

Jochen Kalmbach - 20 Oct 2004 07:05 GMT
> I think they use "CP_ACP" (but I am not sure.. maybe I will dig into
> it...)

I looked at it:
It is internally calling "WideCharToMultiByte" with codepage = CP_ACP

The codepage is hardcoded in the source of
"mscorwks!ML_CSTR_C2N_SR::DoConversion"

If you need a different codepade you could use the following code:

<code>
System::String *mstr = S"Hello world!";
char *ustr;

unsigned char c __gc[] =
 System::Text::Encoding::GetEncoding(1252)->GetBytes(mstr);
unsigned char __pin*cc = &c[0];
ustr = new char[c->get_Length()+1];
strcpy(ustr, (char*) cc);

// now do something with the converted string

delete [] ustr;
</code>

Signature

Greetings
 Jochen

  My blog about Win32 and .NET
  http://blog.kalmbachnet.de/

Hesse - 20 Oct 2004 16:49 GMT
Thanks Jochen! That answered my questions!

> > I think they use "CP_ACP" (but I am not sure.. maybe I will dig into
> > it...)
[quoted text clipped - 21 lines]
> delete [] ustr;
> </code>
Hesse - 20 Oct 2004 18:17 GMT
Hi,

Does Marshal.PtrToStringAnsi do the exact reverse of
Marshal.StringToHGlobalAnsi, that is, does it call MultiByteToWideChar with
the CP_ACP code page?

Thanks!

> > I think they use "CP_ACP" (but I am not sure.. maybe I will dig into
> > it...)
[quoted text clipped - 21 lines]
> delete [] ustr;
> </code>
Jochen Kalmbach - 20 Oct 2004 18:22 GMT
Hi =?Utf-8?B?SGVzc2U=?=,

> Does Marshal.PtrToStringAnsi do the exact reverse of
> Marshal.StringToHGlobalAnsi, that is, does it call MultiByteToWideChar
> with the CP_ACP code page?

I have not verified, but according to the doc: yes.

Signature

Greetings
 Jochen

  My blog about Win32 and .NET
  http://blog.kalmbachnet.de/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.