Binary Files and C#

initroot

Senior Member
Joined
Jul 30, 2011
Messages
898
Reaction score
45
Location
Cape Town
I have now loaded the Binary file into a byte array and converted it to string. Here is an example of how the string would look:

537465766554686F6D70736F6E07200F0707BA024A00FF494845433A4B4838410C485855545352344B552B23424A64323202080A0C52050606317486233E001317010F0801010201010101020101010101010101010101010101010101010101010101010101090101080F0109090908080909080108080108010F42656E466F64656E0720160707C10F090B4800CA4B42524F4C48444B553D4B41380A03054A3C554A524B4064323204065205030606317186373E001317010F0801010209080801020101010101010101010101010101010101010101010101010101010101080F0108080108080809080808080908010F
I want to search this string for the pattern: 08010F

I then want to extract the string between the patterns.

Code:
openFileDialog1.ShowDialog();
            string path = openFileDialog1.FileName;
             FileStream fs = new FileStream(path, FileMode.Open);
                BinaryReader br = new BinaryReader(fs);
                int length = (int)fs.Length;
               
                br.BaseStream.Position = 0x0000E48E;
               byte[] input = br.ReadBytes(length);
               int pos1 = 0;
               int pos2 = 0;
               string sPattern = "08010F";

              
                
                string converted = BitConverter.ToString(input).Replace("-", null);
Please Help
 
For me easy in ZX Spectrum basic. I did it in those days. :-)
 
Use a regular expression, see the System.Text.RegularExpressions.Regex class
 
Hi, thanks i have now extracted the strings using :
string path = openFileDialog1.FileName;
FileStream fs = new FileStream(path, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
int length = (int)fs.Length;
textBox1.Text = path;
br.BaseStream.Position = 0x0000E48E;
byte[] input = br.ReadBytes(length - 1);
int pos1 = 0;
int pos2 = 0;
string sPattern = "08010F";

System.IO.StreamWriter sw = new System.IO.StreamWriter(database);
string converted = BitConverter.ToString(input).Replace("-",null);
Cursor.Current = Cursors.WaitCursor;
while (converted.Contains(sPattern))
{

int FirstChr = converted.IndexOf(sPattern);
sw.WriteLine(converted.Substring(pos1, FirstChr + 6));



converted = converted.Remove(0, FirstChr + 6);
}
sw.Close();
It now outputs:
47656F7267654368757465720720090707B8024600DF3D503A2A363D3D26410E4A505052554533425151452B73013E001919010F0801010201080801020101010101010101010101010101010101010101010101010808010108080F0108090908080909080808080108010F

44616E6E7947726577636F636B07044E01074A5440382B4B461641127D1B3E001319010F0801010201080801020101010101010101010101010101010101010101010101010808010101080F0107010808010109080101080908010F

in a line in my text file.
I now want to extract the Name of the player from this. How do i do that? Oh and this is a roster file.
I am currently extracting this using:private string HexString2Ascii(string hexString)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= hexString.Length - 2; i += 2)
{
sb.Append(Convert.ToString(Convert.ToChar(Int32.Parse(hexString.Substring(i, 2), System.Globalization.NumberStyles.HexNumber))));
}
return sb.ToString();
}
But i only want to read the Ascii symbols not the mixed characters.
 
If its a string already then use indexOf and lastIndexOf to get the first and last positions of 08010F then use substring to chop out the bit you want ?
 
I already did that. The 47656F7267654368757465720720090707B8024600DF3D503A 2A363D3D26410E4A505052554533425151452B73013E001919 010F0801010201080801020101010101010101010101010101 010101010101010101010808010108080F0108090908080909 080808080108010F contains a name. this is in the first part and i don't know the lenght of that name. i want to extract the name.
If its a string already then use indexOf and lastIndexOf to get the first and last positions of 08010F then use substring to chop out the bit you want ?
 
Top
Sign up to the MyBroadband newsletter
X