1

I want to parse attached binary file. Binary files contains multiple records having same structure .Therefore i have set some chunk to differentiate between multiple records.But this is File dependent..

Tried below code but it is not generic for all files:

  static List<string> SplitString(int chunk, string input)
    {
        List<string> list = new List<string>();
        int cycles = input.Length / chunk;

        if (input.Length % chunk != 0)
            cycles++;

        for (int i = 0; i < cycles; i++)
        {
            try
            {
                if ((i + 1) * chunk < input.Length)
                    list.Add(input.Substring(i * chunk, chunk));
                else
                    list.Add(input.Substring(i * chunk));
            }
            catch
            {

            }
        }
        return list;
    }

Binary File

Bhushan Muttha
  • 420
  • 2
  • 11
  • You probably shouldn't be dealing with `string`s here, but byte arrays. Only when you have actually textual data, parse them to strings. – AKX Jul 17 '19 at 07:24
  • 3
    what is the question here? AKX is 100% right that you shouldn't be dealing with `string` input here - any of `Stream`, `byte[]`, `Span` etc would be fine, but if you start with `string` : your data is already toast. But... what is the question? – Marc Gravell Jul 17 '19 at 07:26
  • 1
    My question is . Is there any way to read binary files with dynamic class and firstly i am reading file into byte array byte[] byteBuffer = File.ReadAllBytes(RESTAppPath); – Bhushan Muttha Jul 17 '19 at 07:26
  • 1
    [Binary file](https://en.wikipedia.org/wiki/Binary_file) simply means non-text file. You can't *parse* any binary file, because you must know its structure. Is your question rather how to [show byte array as string](https://stackoverflow.com/q/311165/1997232)? Or what you mean when saying *"file dependent"*? – Sinatr Jul 17 '19 at 07:27
  • Yes Absoulte right Sinatr , We cannot parse Binary file we have to know the structure. that's was my concerns – Bhushan Muttha Jul 17 '19 at 07:30
  • 2
    We don't know what the layout of your binary files is. – CodeCaster Jul 17 '19 at 07:30
  • 1
    Where do these files come from? They must have some sort of structure that can be known ahead of time? – phuzi Jul 17 '19 at 07:42
  • These files are data files which is been outsource to get the data from those . I also do not have an exact idea regarding layout . so I am just trying by work around – Bhushan Muttha Jul 17 '19 at 08:21
  • 1
    If you don't know the structure of the binary files then you cant parse them. Its like asking to build a specific house without a blueprint. Your best bet is to try and reverse engineer the files to build some form of a standard format. – Kieran Devlin Feb 10 '20 at 12:05
  • Yes thanks , got the structure of file so it got resolved . – Bhushan Muttha Feb 10 '20 at 12:38

0 Answers0