CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Binary Files

  1. #1
    Join Date
    Dec 2010
    Posts
    19

    Binary Files

    Hi..
    I have a BinaryFile, actualy an xbox roster file. I need to read and write from it.
    I want to start reading it from a certain hex pattern until the next hex pattern etc. throughout the file.

    Information:
    ...Nicol..sVergallo. .....E..GHKKIYQSVIHG....M7WLKFHd22.R.......1z.[>.............................................................
    EstebanLozada..O..3I@9&...A.UG=V.Z=JEQ?.Gd22..R.?..........1t.Y>.............................................................
    This is where 2 players is stored, before each player name the following pattern occurs: 08 01 0F. I want to read between each pattern.

    Thanks
    Frans

  2. #2
    Join Date
    Jun 2011
    Posts
    35

    Re: Binary Files

    Hi,

    I have no experience with this kind of format of file but this appears to be a text file?

    What kind of information is it you want to extract/write to that file? Also what do the information you quoted from the file represent?

    If this is a text file and not just some garbage copied from a binary file then yes, you should be able to write a parser for that file.

    Hope I could help


    Laurent

  3. #3
    Join Date
    Dec 2010
    Posts
    19

    Re: Binary Files

    This is data copied from a binary file. I can read the whole file in a byte array.

    I only want to read each player. The binary for that values are:4E69636F6CC3A17356657267616C6C6F06201E0C07CF094500AA47484B4B49595153564948470C0705094D37574C4B46486432320252050203020E0206317A845B3E001B19010D0809090901080801020101010101010101010101010101010101010101010101010101010108080F0107090808080909080801080108010F
    As you can see after each player the binary pattern: 80108010F repeats itself. i need to extract the values between these patterns
    4573746562616E4C6F7A61646106044F00FE33494039261C2E17410455473D56085A3D4A45513F1E47643232020C52053FE0000000000000030406317484593E001919010D0801010201010801020101010101010101010101010101090901010101010101010101010101080F01090908080809090808080 80108010F

  4. #4
    Join Date
    Dec 2010
    Posts
    19

    Re: Binary Files

    I have now loaded the 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.
    Please Help

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Binary Files

    Become familiar with the String class in msdn and what it has to offer. With a string, you can use IndexOf to search for a sequence of characters within a string.

    Code:
                // Byte array simulating the file load
                var a = new byte [ ] { 0x11, 0x2f, 0x08, 0x01, 0x0f, 0x18, 0x11, 0x2f, 0x08, 0x01, 0x0f, 0x18, 0x11, 0x2f };
                
                var fileData = Encoding.ASCII.GetString(a);
    
                var bytesToSearch = Encoding.ASCII.GetString(new byte [ ] { 0x08, 0x01, 0x0f} );
    
                // Searches at the beginning of the string - returns index 2 (finds the 1st byte series)
                var index = fileData.IndexOf(bytesToSearch, 0);
    
                // Searches 1 char past the beginning of the string - returns 8 (gets the 2nd byte series)
                index = fileData.IndexOf( bytesToSearch, index + 1 );

  6. #6
    Join Date
    May 2007
    Posts
    1,546

    Re: Binary Files

    Quote Originally Posted by Arjay View Post
    Become familiar with the String class in msdn and what it has to offer. With a string, you can use IndexOf to search for a sequence of characters within a string.
    You should also check how to convert bytes to strings without data loss See this link for ascii encoding: http://msdn.microsoft.com/en-us/libr...ing.ascii.aspx . Here is the relevant quote:

    The ASCIIEncoding object that is returned by this property might not have the appropriate behavior for your application. It uses replacement fallback to replace each string that it cannot encode and each byte that it cannot decode with a question mark ("?") character.
    Don't ever use UTF8, ASCII, UTF16 etc if you have a random array of bytes that you want to represent as a string. Base64 encode it if it must be treated as a string, otherwise you *will* have data loss.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  7. #7
    Join Date
    Dec 2010
    Posts
    19

    Re: Binary Files

    Thanks alot. Solved it already. Here is what i used, any comments on it?
    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 - 1);
    int pos1 = 0;
    int pos2 = 0;
    string sPattern = "08010F";


    string converted = BitConverter.ToString(input).Replace("-", null);
    while (converted.Contains(sPattern))
    {
    int FirstChr = converted.IndexOf(sPattern);
    MessageBox.Show((FirstChr + 6).ToString());
    //SHOWS START POSITION OF STRING
    lstPlayers.Items.Add(converted.Substring(pos1, FirstChr + 6));
    converted = converted.Remove(0, FirstChr + 6);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured