从BIN读取时将十六进制值转换为字符串

时间:2012-07-27 22:09:05

标签: c# parsing binary

我正在用C#编写一个程序来修改和操作二进制文件以用于汽车计算......当我试图向用户显示特定地址的十六进制值时,我遇到了一个问题(我已经得到了程序来根据需要修改文件,现在我试图让它更加用户友好)我已经得到程序将HEX中的整个文件写入textBox,但由于某种原因我在尝试显示一个时遇到问题值。这是代码,您可以看到我已经注释掉了用于显示整个文件的部分...

// Opens the stream, notice that this case only has.Read appended to it, we
// don't want to harm the original binary file
using (FileStream stream = new FileStream(chosenFile, FileMode.Open, FileAccess.Read))
{
    // returns the number of bytes in the file...
    size = (int)stream.Length; 
    // Initializes the array in which to store the data bytes...
    data = new byte[size];
    // Writes the binary data to a particular file...
    // In this case the data[] array
    stream.Read(data, 0, size); 
    progressBar1.Maximum = size;

    // Prints the original binary file...
    while (printCount < size) 
    {
        int i = data[printCount];
        // Formats the data to the same format as the hex editor...
        // not sure exactly why it works...
        // string h = i.ToString("x2"); 
        // richTextBox1.Text += h + "-";

        if (printCount1 == 15)
        {
            // richTextBox1.AppendText(Environment.NewLine);
            printCount1 = 0;
        }
        if (stream.Position == 32)
        {
            pinByte1 = i.ToString("x2");
        }
        if (stream.Position == 33)
        {

            pinByte2 = i.ToString();
            // Now stores the reversed location of the two,
            // convert to decimal for pin...
            pin = pinByte2 + pinByte1;

        }

        printCount++;
        printCount1++;
        progressBar1.Increment(1);

        textBox1.Text = "Please wait, reading original BIN...";
    }
    textBox1.Clear();
    textBox1.Text = "Your file has been read, proceed to unlock...";
    textBox2.Text += pinByte1;
    textBox3.Text += pinByte2;
    textBox6.Text += pin;
}

0 个答案:

没有答案