第三个字节位置确定该字符串的长度.然后我想在每个字节位置的末尾添加一个“:”

本文关键字:字节 位置 添加 一个 三个 字符串 然后 | 更新日期: 2023-09-27 18:30:58

void Button_9600Click(object sender, System.EventArgs e)
{
    //Assigning filename to text box
    string filename = textBox4.Text;
    if ((filename.Length == 0) || (!File.Exists(filename)))
    {
        MessageBox.Show("Error: Invlaid path");
        return;
    }
    string[]lines =File.ReadAllLines(filename);
    for(int i = 0; i < lines.Length; i++)
    {
        string line = lines[i].Replace("00","00:");
        //string Dquote = lines[i].Replace("'"","'");
        if(line.Contains("00 41"))
        {
            line = line.Replace("00 41","00: 41");
            //frags.Add(line.Trim());
        }
    }
    StringBuilder sb = new StringBuilder();
    string tmp = "";
    sb.AppendLine( tmp.Trim('#'));
    File.WriteAllText(filename+".9600.sdf", sb.ToString());

这是我要转换的文件

41 2B 06 01 73 00 41 AB 0D 48 01 CF FC 80 CF FC 80 D8 05;
41 2B 06 03 75 00 41 AB 17 48 03 0A 0A 11 EF 05 02 14 1E 05 08 28 FF 03 50 00 7F A1 04;
41 2B 06 04 76 00 41 AB 17 48 04 00 00 7F 7F 12 00 00 00 00 00 00 69 18 03 01 05 E9 02;
41 2B 06 05 77 00 41 AB 17 48 05 01 02 14 1E 32 0A 03 64 0A 05 10 05 05 1E 32 05 A6 02;
41 2B 06 06 78 00 41 AB 17 48 06 00 19 78 03 1E 08 0F 09 1E 1E 01 01 28 14 71 00 0E 03;
41 2B 06 07 79 00 41 AB 17 48 07 00 3D 07 2B 06 0F 15 38 70 BB 14 28 96 FA 00 FA 14 06;

第三个字节告诉我字符串会有多长所以我想要在位置 6 添加一个":"像下面一样

41 2B 06 01 73 00: 41 AB 0D 48 01 CF FC 80 CF FC 80 D8 05;

第三个字节位置确定该字符串的长度.然后我想在每个字节位置的末尾添加一个“:”

这个怎么样:

void updatefile(string filename)
{
    char[] buffer = new char[17];
    //string tempfile = Path.GetTempFileName();
    string newfile = filename + ".9600.sdf";
    //use tempfile and uncomment lines to replace the existing file.
    StreamWriter writer = new StreamWriter(newfile); 
    StreamReader reader = new StreamReader(filename);
    while (!reader.EndOfStream)
    {
        writer.Write(buffer, 0, reader.Read(buffer, 0, 17));
        writer.Write(":");
        if (!reader.EndOfStream)
            writer.WriteLine(reader.ReadLine());
    }
    writer.Close();
    reader.Close();
    //File.Copy(tempfile, filename, true);
    //File.Delete(tempfile);
}