BinaryWriter问题

编程入门 行业动态 更新时间:2024-10-27 22:20:05
本文介绍了BinaryWriter问题-“代码在Write()方法之间添加了一些字节"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试使用BinaryWriter然后使用BinaryReader编写一些代码. 当我想写时,我使用方法Write(). 但是问题在于,在两行Write方法之间出现了一个新的字节,该字节在ASCII表中为十进制31(例程24). 您可以在这张图片上看到它:

I am try to do some code using BinaryWriter and Then BinaryReader. When I wanna write I use method Write(). But the problem is that between two lines of Write method there appears a new byte which is in ASCII table in decimal 31 (sometines 24). You can see it on this image:

您会看到索引4(第5个字节)处的字节是ASCII十进制值31.我没有在此处插入.如您所见,第1个4个字节保留给一个数字(Int32),其次是其他数据(主要是一些文本-现在这并不重要).

You can see that byte at index 4 (5th byte) is of ASCII decimal value 31. I didnt insert it there. As you can see 1st 4 bytes are reserved for a number (Int32), next are other data (some text mostly - this is not important now).

从我写的代码中可以看到: -进入第一行的数字为10 -在第二行文字这是一些文字..."

As you can see from the code i write: - into 1st line a number 10 - into 2nd line text "This is some text..."

介于这之间的第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) 的文档,您会看到它写了一个长度前缀的字符串.所以31是字符串中的字符数-完全正常.

If you look at the documentation for Write(string), you'll see that it writes a length-prefixed string. So the 31 is the number of characters in your string -- perfectly normal.

更多推荐

BinaryWriter问题

本文发布于:2023-05-25 00:27:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/218688.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:BinaryWriter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!