0

Please look at the code below. This program simply saves a 33-character-length string "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" with an additional byte value of "33".

using System.Text;

namespace Test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string filepath = args[0];
            using (var stream = File.Open(filepath, FileMode.Create))
            {
                using (var writer = new BinaryWriter(stream, Encoding.UTF8, false))
                {
                    writer.Write(new string('!', 33));
                    writer.Write((byte)33);
                }
            }

            using (var stream = File.Open(filepath, FileMode.Open))
            {
                using (var reader = new BinaryReader(stream, Encoding.UTF8, false))
                {
                    Console.WriteLine(reader.ReadString());
                    Console.WriteLine(reader.ReadByte());
                }
            }

            Console.ReadKey();
        }
    }
}

And here is the binary representation of it:

Binary Representation of Writing Result

Apparently, the first starting "ox21" is the length of the string - but how on earth does C# know?

  • It knows because it is [*always*](https://referencesource.microsoft.com/#mscorlib/system/io/binarywriter.cs,362) the first byte(s). https://stackoverflow.com/a/31501941/17034 – Hans Passant Sep 09 '22 at 21:49
  • Thanks for the insight - I guess the key to my question is that it might be using some sort of "7-bit encoding" as mentioned in the link. That's how it differentiates between the first bytes representing length of string to the rest of the values. – Tom Charles Zhang Sep 13 '22 at 18:13

0 Answers0