is char unicode?

  • Thread starter Thread starter Claire
  • Start date Start date
C

Claire

Hi :)
I thought chars were unicode nowadays. If so then why does the following
return 1 as size of the struct please.

using System.Runtime.InteropServices;
public struct s
{
public char j;
}

private void Form1_Load(object sender, EventArgs e)
{
s ss = new s();
Text = Marshal.SizeOf(ss).ToString();
}
 
Claire said:
Hi :)
I thought chars were unicode nowadays. If so then why does the following
return 1 as size of the struct please.

A guess: You marshal to some encoding, that is able to use a single byte for
some chars. Example: utf8.

Regards,

Mads

--
Med venlig hilsen/Regards

Systemudvikler/Systemsdeveloper cand.scient.dat, Ph.d., Mads Bondo
Dydensborg
Dansk BiblioteksCenter A/S, Tempovej 7-11, 2750 Ballerup, Tlf. +45 44 86 77
34
 
Mads Bondo Dydensborg said:
A guess: You marshal to some encoding, that is able to use a single byte
for
some chars. Example: utf8.

I havent set any encoding, the sample code is what I used in a new
application. I'm a bit confused why the size of the struct is 1 rather than
2 if the visual studio help hint that appears when my mouse passes over
"char" says "struct system.char represents a unicode character"

I'm creating some byte[] arrays of data to write to smartcards and need to
know how to create a struct that defines unicode char[] arrays rather than 1
byte char[] arrays
 
I thought chars were unicode nowadays. If so then why does the following
return 1 as size of the struct please.

It's not returning 1 as the size of the struct - it's returning 1 as
the size of the *marshalled* data, for situations where it's
marshalled to unmanaged code.

If you use the sizeof(...) operator it will return 2.

Jon
 
Claire said:
I havent set any encoding,

Whenever you Marshal something, an encoding is implied. At least, that is
how I understand it.

Regards,

Mads

--
Med venlig hilsen/Regards

Systemudvikler/Systemsdeveloper cand.scient.dat, Ph.d., Mads Bondo
Dydensborg
Dansk BiblioteksCenter A/S, Tempovej 7-11, 2750 Ballerup, Tlf. +45 44 86 77
34
 
Claire said:
Hi :)
I thought chars were unicode nowadays. If so then why does the following
return 1 as size of the struct please.

using System.Runtime.InteropServices;
public struct s
{
public char j;
}

private void Form1_Load(object sender, EventArgs e)
{
s ss = new s();
Text = Marshal.SizeOf(ss).ToString();
}


This is because Marshal.SizeOf returns the size of the marshaled structure
AND because char is marshaled as Ansi by default.
If you want to marshal chr's as Unicode you'll have to set the CharSet
attribute, like:

[ StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct s
{
public char j;
}

for above Marshal.SizeOf will return 2.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top