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

C#二进制读取不完整字符串,c#,winforms,listview,binaryreader,C#,Winforms,Listview,Binaryreader,我希望能在这个问题上得到一些帮助,所以正如您在下面的图片中所看到的,在0x00之前,我似乎无法阅读整个单词: 我尝试做的是正确读取文件字符串,以下是我用来打开文件的代码: private void menuItem2_Click(object sender, EventArgs e) { listView1.Items.Clear(); textBox1.Text = ""; menuItem12.Text = "file

我希望能在这个问题上得到一些帮助,所以正如您在下面的图片中所看到的,在0x00之前,我似乎无法阅读整个单词:

我尝试做的是正确读取文件字符串,以下是我用来打开文件的代码:

        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;
    }
private void menuItem2\u单击(对象发送方,事件参数e)
{
listView1.Items.Clear();
textBox1.Text=“”;
menuItem12.Text=“文件类型为:”;
OpenFileDialog ofd=新建OpenFileDialog();
ofd.Title=“打开文件”;
ofd.Filter=“所有文件(*.*)|*.*”;
if(ofd.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
path=ofd.FileName;
BinaryReader br=新的BinaryReader(File.OpenRead(path),Encoding.GetEncoding(“SHIFT-JIS”);
foreach(br.ReadChars(4)中的char mychar)menuItem12.Text+=mychar;
if(menuItem12.Text!=“文件类型为:TXTD”)
{
Show(“这不是TXTD文件…”,“对不起”,MessageBoxButtons.OK,MessageBoxIcon.Error);
返回;
}
其他的
{
Show(“文件已成功打开!”,“Info”,MessageBoxButtons.OK,MessageBoxIcon.Information);
br.BaseStream.Position=0x8;
int Pntrnum=br.ReadInt16();
menuItem11.Visible=true;
menuItem11.Text=Pntrnum.ToString();
列表偏移量=新列表();
br.BaseStream.Position=0x10;
对于(int i=0;i
为了将二进制数据转换为正确的字符串,您应该了解。因此,您可以在
System.Text.Encoding
中找到一些有用的编码。然后您可以将字节数组转换为如下所示的字符串:

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

您还可以看到

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

byte[] byteArray = // your byte array
System.Text.Encoding encoding = Encoding.UTF8; //use proper encoding
string value = encoding.GetString(byteArray);
您还可以看到

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

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

stringLength = (nextOffset - currentOffset - 1) / 2;
假定字符占用两个字节。

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

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

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

假设字符占用两个字节。

stringLength=(nextofset-currentcoffset-1)/2
中,为什么要除以2?(我认为“SHIFT-JIS”编码在ASCII范围内具有单字节字符)。谢谢,先生,没有在那里看到它,请将其作为解决方案发布,以便我可以选择:)再次感谢您在
stringLength=(nextofset-currentOffset-1)/2
,为什么要除以2?(我认为“SHIFT-JIS”编码在ASCII范围内有单字节字符)。谢谢您,先生,没有在那里看到它,请将其作为解决方案发布,以便我可以选择:)再次感谢您