BinaryReader.ReadString() 無參數卻能正確讀取字串,因其寫入時 (BinaryWriter.Write(string)) 會先寫入**長度前綴**,採用**可變長度編碼 (7-bit encoded int)**。
1 表示後續還有位元組,0 表示結束。0x05 (單位元組);長度 131 → 0x81 0x03 (第一位元組 MSB=1,第二位元組 MSB=0)。length |= (b & 0x7F) << (7 * count)。protected void Write7BitEncodedInt(int value) {
while (value >= 0x80) {
Write((byte)(value | 0x80));
value >>= 7;
}
Write((byte)value);
}
此設計節省空間:短字串 (長度 < 128) 僅需 1 位元組存長度,無需固定 4 位元組 int。