473,386 Members | 1,758 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,386 software developers and data experts.

is char unicode?

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();
}
Oct 17 '07 #1
7 2031
Claire wrote:
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
Oct 17 '07 #2

"Mads Bondo Dydensborg" <mb*@dbc.dkwrote in message
news:ON**************@TK2MSFTNGP05.phx.gbl...
Claire wrote:
>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.
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

Oct 17 '07 #3
On Oct 17, 11:13 am, "Claire" <c...@nospam.comwrote:
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

Oct 17 '07 #4
If you use the sizeof(...) operator it will return 2.
>
Jon
Thanks Jon :)
Oct 17 '07 #5
Claire wrote:
>
"Mads Bondo Dydensborg" <mb*@dbc.dkwrote in message
news:ON**************@TK2MSFTNGP05.phx.gbl...
>Claire wrote:
>>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.

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
Oct 17 '07 #6
Thanks very much Mads :)
Oct 17 '07 #7
"Claire" <cc@nospam.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...
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.

Oct 17 '07 #8

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

Similar topics

6
by: Markus Hämmerli | last post by:
I ' ll tra to convert a Cstring to char* without success. I working in a Unicode enabled environment this is working in Unicode CString source = _T("TestString"); TCHAR *szSource =...
3
by: Mark Miller | last post by:
I have a char array and when I write it to a file using BinaryWriter the position of the pointer is the size of the array + 1. For example: writing char leaves the pointer at position 26 after...
4
by: Sriniv | last post by:
Hello all, Do we have unsigned and signed char types? If so, i'd like to know the differences between the two. And one more question. Is there any provision in C for using the Unicode character...
10
by: Dan | last post by:
Hi all, I'd like to submit what it seems to be a bug as for the Unicode compliance of methods like Char.Is...: as stated by the latest version of Unicode, codes +03F2 and +03F9 represent Greek...
2
by: Gidi | last post by:
Hi, I'm writing a C# win application program, and i need to transfer my hebrew letters from unicode to ascii, now if i use the ascii encoding it writes me ??? instead of the hebrew letter i've...
5
by: Joe Thompson | last post by:
Hi, I am using VS.net 2003 to write a managed C++ windows application. My main form traps the WM_DEVICECHANGE message and calls a method in my own unmanaged class passing the wParam and lParam....
6
by: NormD | last post by:
I'm sending a string (xml string) to web service as a parameter. One of the tags in the xml string is the address field and the values of this tag have LF + CR chars. When I receive the string in...
1
by: Alexander Higgins | last post by:
>>Thanks for the response.... Point Taken but this is not the case. Thus, if a person writes a text file on her or his computer and does not use UNICODE to save it, the current code page is...
1
by: Simon Posnjak | last post by:
On Mon, May 5, 2008 at 4:16 PM, Jean-Paul Calderone <exarkun@divmod.comwrote: some_module.some_thing(the_string) function is a swig generated function from a C lib. The C lib function expects...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.