我有一個問題寫入二進制文件。C#,寫入二進制數據
//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
,它會寫入63
(3F
錯誤的表示),直到255
。
然後重複這個錯誤,直到512
。
錯誤是什麼?
的問題是,我需要此計數器在一個沖洗添加到消息,並打印出來,當我試圖打印它被打印爲8個字節,而不是4和錯誤值。我在打印前添加了: //這是準備計數器爲binaryint nCounterIn = ...; int nCounterTotalInNetwork = System.Net.IPAddress.HostToNetworkOrder(nCounterIn); byte [] byteFormat = BitConverter.GetBytes(nCounterTotalInNetwork);字符串strV = BitConvertor.ToString(byteFormat); strV + = //消息字符串DATA。 m_brWriter.Write(strV); m_brWriter.Flush(); –