[.net] C# char size
So im curious, why does sizeof report chars as being 2 bytes, but when i write to a binary file they only take up one byte?
Or the encoding is set to UTF-8, which causes characters in the ASCII range to only occupy one byte.
For example, the default setting of BinaryWriter is to use UTF-8. This is a reasonable choice as it will reduce the file size (for texts that use mostly ASCII characters at least). You can make BinaryWriter use a different encoding (e.g. Unicode) by using the appropriate constructor overload.
Regards,
Andre
For example, the default setting of BinaryWriter is to use UTF-8. This is a reasonable choice as it will reduce the file size (for texts that use mostly ASCII characters at least). You can make BinaryWriter use a different encoding (e.g. Unicode) by using the appropriate constructor overload.
Regards,
Andre
It could be worse, BinaryWriter could default to use Encoding.Default ;)
Also isn't UTF-8 more endian friendly ? I know UTF-8 is my prefered choice of file storage.
Also isn't UTF-8 more endian friendly ? I know UTF-8 is my prefered choice of file storage.
What's really crazy is when you are writing applications in C++/CLI which target the .NET framework, and the sizeof() operator returns a different size for char than Marshal.SizeOf().
Quote:Original post by Mike.Popoloski
What's really crazy is when you are writing applications in C++/CLI which target the .NET framework, and the sizeof() operator returns a different size for char than Marshal.SizeOf().
The same happens in C# (Marshal.SizeOf(typeof(char)) vs sizeof(char)). It's caused by the fact that the CLR assumes CharSet.Ansi if the given type has no CharSet in the StructLayout attribute. System.Char has no StructLayoutAttribute.
Regards,
Andre
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement