really need some help with this...
i got a string in a file: SaveStation_001
which is in hex: 53 61 76 65 53 74 61 74 69 6F 6E 5F 30 30 31
I wrote this in C#
private void s_PlayerSStation_Click(object sender, EventArgs e)
{
BinaryWriter m_bw = new BinaryWriter(File.OpenWrite(ofd.FileName));
if (textBox3.Text == "")
{
m_bw.Close();
MessageBox.Show("Please enter a number before pressing the button", "Save Station Error");
return;
}
int x = Convert.ToInt32(textBox3.Text);
if (x > 999)
{
m_bw.Close();
MessageBox.Show("Value exceeds max");
return;
}
if (textBox3.Text.Length < 3)
{
if (textBox3.Text.Length < 2)
{
string p = String.Format("00{0}", textBox3.Text);
m_bw.BaseStream.Position = 0x00000004;
m_bw.Write(p);
m_bw.BaseStream.Position = 0x000038BC;
m_bw.Write(p);
label4.Text = String.Format("You will now spawn a save station: {0}", textBox3.Text);
m_bw.Close();
return;
}
string z = String.Format("0{0}", textBox3.Text);
m_bw.BaseStream.Position = 0x00000004;
m_bw.Write(z);
m_bw.BaseStream.Position = 0x000038BC;
m_bw.Write(z);
label4.Text = String.Format("You will now spawn a save station: {0}", textBox3.Text);
m_bw.Close();
return;
}
m_bw.BaseStream.Position = 0x00000004;
m_bw.Write(textBox3.Text);
m_bw.BaseStream.Position = 0x000038BC;
m_bw.Write(textBox3.Text);
label4.Text = String.Format("You will now spawn a save station: {0}", textBox3.Text);
m_bw.Close();
}
which should only change the number 001 to whatever the user types into textBox3 so lets say i typed in 66 it should just change SaveStation_001
to SaveStation_066
but for some reason it shifts it 1 to the right so it looks like this SaveStation_ 066
(in hex: 53 61 76 65 53 74 61 74 69 6F 6E 5F |03| 30 36 36
). Anyone know what the issue is? also idk where it gets the 03
from in the new hex (marked it with | )
See the documentation of the write call you are making
Writes a length-prefixed string to this stream in the current encoding of the BinaryWriter
Calling BinaryWriter.Write(string)
will encode the string's length at the front of the string before it writes, the extra byte you see is that length byte.
What you need to do is use the BinaryWriter.Write(char[])
overload which will not prefix a length. You can call String.ToCharArray()
to convert it to the array format.
m_bw.Write(p.ToCharArray());
You must first convert your string to binary. The [03] is the length of the string being written.
A conversion function:
public static byte[] ConvertToByteArray(string str, Encoding encoding)
{
return encoding.GetBytes(str);
}
User contributions licensed under CC BY-SA 3.0