0

i was hoping to get some help on this problem, so as you can see in the picture bellow, i can't seem to read the whole word up to 0x00: enter image description here

what i am trying ot do, is to just read the file strings properly ,here is the code i used to open the file:

        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;
    }
Omarrrio
  • 1,075
  • 1
  • 16
  • 34
  • 1
    In `stringLength = (nextOffset - currentOffset - 1) / 2`, why do you divide by 2? (I think that the "SHIFT-JIS" encoding has single-byte chars in the ASCII range). – Matthew Watson Apr 17 '13 at 17:13
  • thank you sir, didn't see it there, please post it as a solution so i can pick it :) thank you again – Omarrrio Apr 17 '13 at 17:19

2 Answers2

2

The "SHIFT-JIS" encoding uses single bytes for characters in the ASCII range.

I can see that your text is ASCII, but this line of code:

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

is assuming that the characters are occupying two bytes.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
1

In order to convert binary data to proper string you should know about Encodings. So you can find some useful encodings in System.Text.Encoding. Then you can convert byte array to string like this:

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

Also you can see the MSDN

Community
  • 1
  • 1
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116