0

I'm having some problems saving the edited data to the original file... I want to save my string from textBox1 to the file it was loaded from.

Here is my "Load" function:

public static string getItemName(int index)
{
    FileStream str = File.OpenRead(Directory.GetCurrentDirectory() + ybi);
    BinaryReader breader = new BinaryReader(str);
    breader.BaseStream.Position = itemSectionStart;
    byte[] itemSection = breader.ReadBytes(itemSectionEnd);

    string itemName = BitConverter.ToString(itemSection, 808 * index + 7, 64).Replace("00", "").Replace("-", "");
    return hex2ascii(itemName);

}

Here is my "Save" function:

public static bool setItemName(int index, string _FileName, byte[] _ByteArray)
{
    try
    {
        System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
        _FileStream.Write(_ByteArray, 808 * index + 7, _ByteArray.Length);
        _FileStream.Close();
        return true;
    }
    catch (Exception _Exception)
    {
        MessageBox.Show(Convert.ToString(_Exception.Message));
    }
    return false;
}

Now, I think here is where the problem is, in the conversion from my HEX String to ByteArray...

private byte[] HexStringToByteArray(string hexString)
{
    int hexStringLength = hexString.Length;
    byte[] b = new byte[hexStringLength / 2];
    for (int i = 0; i < hexStringLength; i += 2)
    {
        int topChar = (hexString[i] > 0x40 ? hexString[i] - 0x37 : hexString[i] - 0x30) << 4;
        int bottomChar = hexString[i + 1] > 0x40 ? hexString[i + 1] - 0x37 : hexString[i + 1] - 0x30;
        b[i / 2] = Convert.ToByte(topChar + bottomChar);
    }
    return b;
}
private void button2_Click(object sender, EventArgs e)
{
    int index = listBox1.SelectedIndex;
    string hex = "";
    foreach (char c in textBox1.Text)
    {
        int tmp = c;
        hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
    }
    writeValuePositions.setItemName(index, save_FileName, HexStringToByteArray(hex.ToUpper()));
}

The byteArray that gets sent to writeValuePositions.setItemName isnt right I think... I get this Exception

---------------------------

---------------------------
Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
---------------------------
OK   
---------------------------
4

2 回答 2

0

试试这个:

private void button2_Click(object sender, EventArgs e)
{
    writeValuePositions.setItemName(index, save_FileName, HexStringToByteArray(textBox1.Text);
}

private byte[] HexStringToByteArray(string hexString)
{
   if (String.IsNullOrEmpty(hexString) || hexString.Length % 2 != 0)
   {
      throw new ArgumentException("Invalid parameter.");
   }

   byte[] array = new byte[hexString.Length / 2];

   for (int i = 0; i < hexString.Length / 2; i++)
   {
      array[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
   }
   return array;
}

请记住约翰桑德斯在他的评论中向您提出的建议。

于 2012-04-30T20:27:20.787 回答
0

我在 setItemName 中看到您的行存在潜在问题:

_FileStream.Write(_ByteArray, 808 * index + 7, _ByteArray.Length);

第二个参数是起始偏移量,第三个参数是要写入的字符总数。看起来您正在编写 808 * index + 7 个字节,并传递了数组的末尾。如果要写出数组的末尾,请从计数中减去偏移量。

于 2012-04-30T20:21:27.057 回答