C# 用于动态短数组的BinaryReader和BinaryWriter
我制作了一个工具,用来加密和解密消息。我不是在为军队工作,也不是什么,我只是对一个概念感兴趣 它接受每个字符并将其转换为我的个人二进制格式。每个字符扩展到16位,或一个C# 用于动态短数组的BinaryReader和BinaryWriter,c#,binary,short,binaryreader,binarywriter,C#,Binary,Short,Binaryreader,Binarywriter,我制作了一个工具,用来加密和解密消息。我不是在为军队工作,也不是什么,我只是对一个概念感兴趣 它接受每个字符并将其转换为我的个人二进制格式。每个字符扩展到16位,或一个短字符。每个短整数都存储在一个数组中 我的目标是将该数组写入二进制文件,并能够将其读回数组 以下是我的开始: //This is the array the encrypted characters are stored in. short[] binaryStr = new short[32767]; //... priva
短字符
。每个短整数都存储在一个数组中
我的目标是将该数组写入二进制文件,并能够将其读回数组
以下是我的开始:
//This is the array the encrypted characters are stored in.
short[] binaryStr = new short[32767];
//...
private void butSelInput_Click(object sender, EventArgs e)
{
dialogImportMsg.ShowDialog();
}
private void dialogImportMsg_FileOk(object sender, CancelEventArgs e)
{
using (BinaryReader reader = new BinaryReader(new FileStream(dialogImportMsg.FileName, FileMode.Open)))
{
for (short x = 0; x < (short)reader.BaseStream.Length; x++)
{
binaryStr[x] = reader.ReadInt16();
}
}
}
private void butExport_Click(object sender, EventArgs e)
{
dialogExportMsg.ShowDialog();
}
private void dialogExportMsg_FileOk(object sender, CancelEventArgs e)
{
using (BinaryWriter writer = new BinaryWriter(new FileStream(dialogExportMsg.FileName, FileMode.OpenOrCreate)))
{
for (int x = 0; x < binaryStr.Length; x++)
{
//if(binaryStr[x]
writer.Write(BitConverter.GetBytes(binaryStr[x]));
}
}
}
//这是存储加密字符的数组。
short[]binaryStr=新的short[32767];
//...
私有void butsel输入\单击(对象发送者,事件参数e)
{
dialogImportMsg.ShowDialog();
}
私有无效对话框importsg_FileOk(对象发送方,取消事件参数e)
{
使用(BinaryReader=新的BinaryReader(新文件流(dialogImportsg.FileName,FileMode.Open)))
{
用于(短x=0;x<(短)reader.BaseStream.Length;x++)
{
binaryStr[x]=reader.ReadInt16();
}
}
}
私有无效但导出单击(对象发送者,事件参数e)
{
dialogExportMsg.ShowDialog();
}
私有无效对话框ExportMsg_FileOk(对象发送方,CancelEventArgs e)
{
使用(BinaryWriter writer=new BinaryWriter(new FileStream(dialogExportMsg.FileName,FileMode.OpenOrCreate)))
{
for(int x=0;x
很明显,我的想法是错误的,因为它没有像我希望的那样工作。写入程序可能在工作,但它写入了整个数组,即65534字节。我希望它只写入存储的字符(即,直到最后一个非零字符为止的所有字符)。然后读取器应与之对应,从文件中读取字符,并将其完全按照导出时的方式放入数组中
所以问题是,我如何做到这一点?我决定只存储原始字符串的长度。我将使用它进行写入循环,将其存储在文件中,然后从文件中读取
private void dialogImportMsg_FileOk(object sender, CancelEventArgs e)
{
using (BinaryReader reader = new BinaryReader(new FileStream(dialogImportMsg.FileName, FileMode.Open)))
{
binaryStrLen = reader.ReadInt16();
for (short x = 0; x < binaryStrLen; x++)
{
binaryStr[x] = reader.ReadInt16();
}
}
}
private void butExport_Click(object sender, EventArgs e)
{
dialogExportMsg.ShowDialog();
}
private void dialogExportMsg_FileOk(object sender, CancelEventArgs e)
{
using (BinaryWriter writer = new BinaryWriter(new FileStream(dialogExportMsg.FileName, FileMode.OpenOrCreate)))
{
writer.Write(BitConverter.GetBytes(binaryStrLen));
for (int x = 0; x < binaryStrLen; x++)
{
writer.Write(BitConverter.GetBytes(binaryStr[x]));
}
}
}
private void对话框importsg\u FileOk(对象发送方,CancelEventArgs e)
{
使用(BinaryReader=新的BinaryReader(新文件流(dialogImportsg.FileName,FileMode.Open)))
{
binaryStrLen=reader.ReadInt16();
对于(短x=0;x
if(binaryStr[x])是您在尝试使用的if(binaryStr[x])
,它不起作用吗?我本来打算尝试提前终止循环…只是不知道该怎么做。是的,那么你在这方面遇到了什么问题?没有问题,因为我没有完成尝试。正如我所说,我不知道该做什么。这就是我来这里的时候。你可以继续(跳过第x个短值)或完全中断
循环。