[英]BinaryWriter problem - “code adds some byte between Write() method”
我尝试使用BinaryWriter和然后BinaryReader做一些代码。 当我想写时,我使用方法Write()。 但是问题是在两行Write方法之间出现了一个新的字节,该字节在ASCII表中为十进制31(例程24)。 您可以在此图像上看到它:
您可以看到索引4(第5个字节)处的字节是ASCII十进制值31。 我没有在此处插入它 。 如您所见,第1个4个字节保留给数字(Int32),其次是其他数据(主要是一些文本-现在并不重要)。
正如您从代码中看到的那样,我写道:-在第一行中输入数字10-在第二行中输入文本“这是一些文本...”
中间的第5个字节(12月31日)是怎么来的?
这是我的代码:
static void Main(string[] args)
{
//
//// SEND - RECEIVE:
//
SendingData();
Console.ReadLine();
}
private static void SendingData()
{
int[] commandNumbers = { 1, 5, 10 }; //10 is for the users (when they send some text)!
for (int i = 0; i < commandNumbers.Length; i++)
{
//convert to byte[]
byte[] allBytes;
using (MemoryStream ms = new MemoryStream())
{
using (BinaryWriter bw = new BinaryWriter(ms))
{
bw.Write(commandNumbers[i]); //allocates 1st 4 bytes - FOR MAIN COMMANDS!
if (commandNumbers[i] == 10)
bw.Write("This is some text at command " + commandNumbers[i]); //HERE ON THIS LINE IS MY QUESTION!!!
}
allBytes = ms.ToArray();
}
//convert back:
int valueA = 0;
StringBuilder sb = new StringBuilder();
foreach (var b in GetData(allBytes).Select((a, b) => new { Value = a, Index = b }))
{
if (b.Index == 0) //1st num
valueA = BitConverter.ToInt32(b.Value, 0);
else //other text
{
foreach (byte _byte in b.Value)
sb.Append(Convert.ToChar(_byte));
}
}
if (sb.ToString().Length == 0)
sb.Append("ONLY COMMAND");
Console.WriteLine("Command = {0} and Text is \"{1}\".", valueA, sb.ToString());
}
}
private static IEnumerable<byte[]> GetData(byte[] data)
{
using (MemoryStream ms = new MemoryStream(data))
{
using (BinaryReader br = new BinaryReader(ms))
{
int j = 0;
byte[] buffer = new byte[4];
for (int i = 0; i < data.Length; i++)
{
buffer[j++] = data[i];
if (i == 3) //SENDING COMMAND DATA
{
yield return buffer;
buffer = new byte[1];
j = 0;
}
else if (i > 3) //SENDING TEXT
{
yield return buffer;
j = 0;
}
}
}
}
}
如果查看Write(string)
的文档 ,您会发现它写了一个长度前缀的string 。 所以31是您字符串中的字符数-完全正常。
您可能应该使用Encoding.GetBytes ,然后写字节而不是写字符串
例如
bw.Write(
Encoding.UTF8.GetBytes("This is some text at command " + commandNumbers[i])
);
将字符串写入二进制流时,它要做的第一件事就是写入字符串的长度。 字符串“这是命令10中的某些文本”具有31个字符,这是您看到的值。
该字节存在的原因是因为您要添加可变数量的信息,因此需要长度。 如果要添加两个字符串,那么您将在哪里知道第一个结束和第二个开始的位置?
如果您确实不需要或不需要该长度的字节,则始终可以将字符串转换为字节数组并使用它。
好的,这是我编辑的代码。 我删除了BinaryWriter(虽然BinaryReader还在!!),现在工作得很好-没有更多的额外字节了。
你什么事 有没有更好的方法来使它运行得更快? 我特别喜欢foreach循环,该循环从另一个方法中读取,该方法是yield return类型!
新代码:
static void Main(string[] args)
{
//
//// SEND - RECEIVE:
//
SendingData();
Console.ReadLine();
}
private static void SendingData()
{
int[] commands = { 1, 2, 3 };
// 1 - user text
// 2 - new game
// 3 - join game
// ...
for (int i = 0; i < commands.Length; i++)
{
//convert to byte[]
byte[] allBytes;
using (MemoryStream ms = new MemoryStream())
{
// 1.st - write a command:
ms.Write(BitConverter.GetBytes(commands[i]), 0, 4);
// 2nd - write a text:
if (commands[i] == 1)
{
//some example text (like that user sends it):
string myText = "This is some text at command " + commands[i];
byte[] myBytes = Encoding.UTF8.GetBytes(myText);
ms.Write(myBytes, 0, myBytes.Length);
}
allBytes = ms.ToArray();
}
//convert back:
int valueA = 0;
StringBuilder sb = new StringBuilder();
foreach (var b in ReadingData(allBytes).Select((a, b) => new { Value = a, Index = b }))
{
if (b.Index == 0)
{
valueA = BitConverter.ToInt32(b.Value, 0);
}
else
{
sb.Append(Convert.ToChar(b.Value[0]));
}
}
if (sb.ToString().Length == 0)
sb.Append("ONLY COMMAND");
Console.WriteLine("Command = {0} and Text is \"{1}\".", valueA, sb.ToString());
}
}
private static IEnumerable<byte[]> ReadingData(byte[] data)
{
using (MemoryStream ms = new MemoryStream(data))
{
using (BinaryReader br = new BinaryReader(ms))
{
int j = 0;
byte[] buffer = new byte[4];
for (int i = 0; i < data.Length; i++)
{
buffer[j++] = data[i];
if (i == 3) //SENDING COMMAND DATA
{
yield return buffer;
buffer = new byte[1];
j = 0;
}
else if (i > 3) //SENDING TEXT
{
yield return buffer;
j = 0;
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.