.net c#File.WriteAllBytes不写入所有字节的一半

stszievb  于 2023-02-26  发布在  .NET
关注(0)|答案(1)|浏览(101)

我有这个功能,应该保存到文件字节.

public static void Build4BB()
{
    string project = Directory.GetCurrentDirectory() + "\\Projects\\P" + Program.CurrentProject + ".b";
    if (Program.CurrentProject == "-") 
    { 
        Misc.CastAError("choose the project first."); 
        return; 
    }
    if (!File.Exists(project)) 
    { 
        Program.Error("This project does not exist."); 
        return; 
    }
    string bytes = File.ReadAllText(project);
    byte[] byteArray = new byte[512];
    for (int i = 0; i < 512; i+=2)
    {
        byteArray[i] = Convert.ToByte(bytes.Substring(0, 4), 2);
        byteArray[i + 1] = Convert.ToByte(bytes.Substring(4, 8), 2);
        bytes = bytes.Substring(12);
        Console.Write(byteArray[i].ToString() + byteArray[i + 1] + " ");
    }

    File.WriteAllBytes(project.Substring(0, project.Length - 1) + "4bb", byteArray);
}

但它仅在以下情况下有效,例如:
FF01艾德00...
但是当第二、第四和第六等为零时,它变为:
关上...
所以它跳过了一半的字节。
我试着重写这个函数,但它不起作用。所以我需要一些帮助。
编辑它的工作原理类似于

但它应该喜欢

toiithl6

toiithl61#

好的,我可以把数组大小设置为513,它会完美地工作。

相关问题