C#二进制读取不完整的字符串

时间:2013-04-17 17:07:32

标签: c# winforms listview binaryreader

我希望能在这个问题上得到一些帮助,所以你可以在下面的图片中看到,我似乎无法将整个单词读到0x00: enter image description here

我正在尝试的是,只是正确读取文件字符串,这是我用来打开文件的代码:

        private void menuItem2_Click(object sender, EventArgs e)
    {
        listView1.Items.Clear();
        textBox1.Text = "";
        menuItem12.Text = "file type is: ";
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Title = "Open File";
        ofd.Filter = "All Files (*.*)|*.*";
        if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            path = ofd.FileName;
            BinaryReader br = new BinaryReader(File.OpenRead(path), Encoding.GetEncoding("SHIFT-JIS"));
            foreach (char mychar in br.ReadChars(4)) menuItem12.Text += mychar;
            if (menuItem12.Text != "file type is: TXTD")
            {
                MessageBox.Show("This is not a TXTD file...", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                    MessageBox.Show("File opened Succesfully!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    br.BaseStream.Position = 0x8;
                    int Pntrnum = br.ReadInt16();
                    menuItem11.Visible = true;
                    menuItem11.Text = Pntrnum.ToString();
                    List<int> offsets = new List<int>();
                    br.BaseStream.Position = 0x10;
                    for (int i = 0; i < Pntrnum; i++)
                    {
                        offsets.Add(br.ReadInt32());
                    }
                    Dictionary<int, string> values = new Dictionary<int, string>();
                    for (int i = 0; i < offsets.Count; i++)
                    {
                        int currentOffset = offsets[i];

                        int nextOffset = (i + 1) < offsets.Count ? offsets[i + 1] : (int)br.BaseStream.Length;

                        int stringLength = (nextOffset - currentOffset - 1) / 2;

                        br.BaseStream.Position = currentOffset;

                        var chars = br.ReadChars(stringLength);
                        values.Add(currentOffset, new String(chars));
                    }

                    foreach (int offset in offsets)
                    {
                        listView1.Items.Add(offset.ToString("X")).SubItems.Add(values[offset]);
                    }

                    br.Close();
                    br = null;
            }
        }
        ofd.Dispose();
        ofd = null;
    }

2 个答案:

答案 0 :(得分:2)

“SHIFT-JIS”编码对ASCII范围内的字符使用单个字节。

我可以看到你的文字是ASCII,但是这行代码:

stringLength = (nextOffset - currentOffset - 1) / 2;

假设字符占用两个字节。

答案 1 :(得分:1)

为了将二进制数据转换为正确的字符串,您应该了解 Encodings 。所以你可以在System.Text.Encoding中找到一些有用的编码。然后你可以将字节数组转换为字符串,如下所示:

byte[] byteArray = // your byte array
System.Text.Encoding encoding = Encoding.UTF8; //use proper encoding
string value = encoding.GetString(byteArray);

您还可以看到MSDN