复制到字节数组的行为不符合预期



我有这个代码:

using System.Text;
var testString = "TestOneString";
var testStringBytes = Encoding.UTF8.GetBytes(testString);
var allBytes = new byte[testStringBytes.Length+2];
allBytes[0] = (byte) testStringBytes.Length;
Console.WriteLine("Length: " + allBytes[0]); // this is 13.
testStringBytes.CopyTo(allBytes,1); // It should be copied from 1 to 13. So the string is allBytes[1] to allBytes[13] or am I wrong?
var printTest = Encoding.UTF8.GetString(allBytes[1..(testStringBytes.Length)]); //allBytes[1..13]
Console.WriteLine(printTest); // this gives back: TestOneStrin
var printTest2 = Encoding.UTF8.GetString(allBytes[1..(testStringBytes.Length+1)]); // why do I need to put the+1 there? this means this is allBytes[1..14]
Console.WriteLine(printTest2); // this gives back: TestOneString (the full thing).
/*However what I don't understand is, if (testStringBytes.Length+1) is 14
* why can I change the 14th byte to anything and the string is still going to print normally? doesn't that mean that 14th byte has nothing to do with the string?
*/
allBytes[testStringBytes.Length + 1] = (byte) (new Random().Next());
var printTest3 = Encoding.UTF8.GetString(allBytes[1..(testStringBytes.Length + 1)]);
Console.WriteLine(printTest3); // this gives back: TestOneString (the full thing).
// So why does it cut when I don't add +1 (which is 14, when the 14th byte has nothing to do with the string??)

对我问题的解释在评论中。

我正在将字符串复制到字节数组。这根绳子的长度是13,我从1开始。所以它应该复制到byteArray[1]。。byteArray[13],但是,当我试图将字节1到13改回字符串时,它会剪切掉最后一个字符。所以我需要做byteArray[1]到14,但是,这没有任何意义,因为第14个字节与字符串无关。(printTest3显示,如果我向第14个字节随机分配任何东西,字符串仍然是满的(。

这是这个程序的控制台输出:

Length: 13
TestOneStrin
TestOneString
TestOneString

有人能向我解释一下,如果第14个字节可以是任何东西,为什么我需要做1到14吗?

在.NET或Java等某些环境中,字符可能占用多个字节。特别适用于UTF-8编码:

此属性返回一个编码Unicode的UTF8Encoding对象(UTF-16编码(字符转换为一到四个字节的序列字符,并将UTF-8编码的字节数组解码为Unicode(UTF-16编码(字符。有关角色的信息.NET支持的编码以及对哪种Unicode编码的讨论要使用,请参阅.NET.中的字符编码

供参考。

因此,您不能假设13个字符的字符串将占用13个字节。

最新更新