0

Hey;

I am trying to keep a data file but I'm getting confused with the sizeof(char) = 2 bytes. I understand that it is a unicode char but why does class BinaryReader's function ReadChars(int count) function just increases the streams position by count bytes ?

If that is not clear here is what I ask;

sizeof(char) = 2 OK!

when I say

BinaryReader fileReader = new BinaryReader(File.Open(fileName, FileMode.Open));
fileReader.ReadChars(100); // Here it increases the current stream position by 100 bytes , but it should be sizeof(char) * 100 = 200 bytes
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Momerath
2

It depends on how the file itself is encoded. If it's an ASCII encoding (UTF-8) then 100 bytes = 100 characters. And just to confuse the issue more, sizeof(char) can vary from 1 to 4 bytes depending on what unicode character is being represented. Most of the time you'll only see the 2 byte version, but there are a few odd characters that require more.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.