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
---------------------------