如何通过特定值提升字节
本文关键字:字节 何通过 | 更新日期: 2023-09-27 18:15:09
我打开了这个文件:
static void Encrypt(string fileName)
{
using (FileStream stream = File.OpenRead(fileName))
{
using (BinaryReader reader = new BinaryReader(stream))
{
for (int i = 0; i < stream.Length; i++)
{
byte b = reader.ReadByte();
byte newByte = (byte(b + 5))
}
}
}
}
我想在我的文件中为每个字节添加特定的值并保存它
所以只需将新字节存储在集合中,并在读取整个文件后保存它们。
var newBytes = new List<byte>();
...
for (int i = 0; i < stream.Length; i++)
{
byte b = reader.ReadByte();
newBytes.Add(b + 5);
}
...
File.WriteAllBytes(filePath, newBytes.ToArray());
你可以这样做:
byte b = reader.ReadByte();
int newNumber = (int)b + 5;
byte newByte = (byte)(newNumber % 256);
为了控制您可能创建的溢出,我建议您将byte
更改为int
。
然后这将5添加到您读取的字节值,当您到达b == 251
时换行为零,如251 + 5 == 256
和256 % 256 == 0
。