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

问题描述 投票:0回答:0

我有这个功能可以将字节保存到文件中。

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);
}

但它仅在以下情况下有效,例如:

FF 01 ED 00...
但是当第二、第四和第六等为零时,它变成:
FF 编...
所以它喜欢跳过所有字节的一半。

我尝试重写函数但它不起作用。 所以我需要一些帮助。

编辑 它像enter image description here

一样工作

不过应该喜欢enter image description here

c# .net file byte
© www.soinside.com 2019 - 2024. All rights reserved.