我特林写为二进制文件的问题。

//This is preparing the counter as binary
int nCounterIn = ...;
int nCounterTotalInNetwork = System.Net.IPAddress.HostToNetworkOrder(nCounterIn);
byte[] byteFormat = BitConverter.GetBytes(nCounterTotalInNetwork);
char[] charFormat = System.Text.ASCIIEncoding.ASCII.GetChars(byteFormat);
string strArrResults = new string(charFormat);

//This is how writing it to a file using a BinaryWriter object

m_brWriter.Write(strArrResults.ToCharArray());
m_brWriter.Flush();

的问题是,它写入文件不正确。它的大部分正确写入信息,但一旦超过127,直到633F255the错误表示)的时间。

然后,它重复这个错误直到512

还有什么错误呢?

有帮助吗?

解决方案

这是因为你与ASCII,它是7位编码它,它会切断第8位,并将其设置为0。

你为什么做这种方式?我试图让我的头左右你在做什么在那里。

为什么你不能简单地写字节数组你,而不是编码呢?

在换句话说,你为什么不使用此代码?

//This is preparing the counter as binary
int nCounterIn = ...;
int nCounterTotalInNetwork = System.Net.IPAddress.HostToNetworkOrder(nCounterIn);
byte[] byteFormat = BitConverter.GetBytes(nCounterTotalInNetwork);
m_brWriter.Write(byteFormat);
m_brWriter.Flush();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top