Reading Binary File containing Strings and Numeric Data

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 4981 Views - Last Post: 04 September 2012 - 11:59 PM Rate Topic: -----

#16 Jogiya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 04-September 09

Re: Reading Binary File containing Strings and Numeric Data

Posted 05 September 2009 - 02:52 PM

Works beautifully.. Thanks a Million!

Finally... All Done!
Was This Post Helpful? 0
  • +
  • -

#17 piman314  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 169
  • Joined: 07-August 09

Re: Reading Binary File containing Strings and Numeric Data

Posted 05 September 2009 - 02:58 PM

you're welcome :D
Was This Post Helpful? 0
  • +
  • -

#18 umsawant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-September 12

Re: Reading Binary File containing Strings and Numeric Data

Posted 04 September 2012 - 11:59 PM

I am trying to read Binary file which i have for created. In this loop 1st time i am able to read first time but when
loop starts at second time its now working propery. I am attaching Binary file which i am trying tom read

        public bool ReadBinaryFile()
        {
            System.IO.FileStream f = new System.IO.FileStream("file.bin", FileMode.Open);
            BinaryReader fin = new BinaryReader(f);
            var taxReturn = new TaxReturn();
            var listEntity = new List<Entity>();
            List<Jurisdiction> jurisdiction = new List<Jurisdiction>();

            while (f.Position < f.Length)
            {
                Entity entity = new Entity();

                var lNameLength = fin.ReadUInt32();
                entity.LName = Encoding.ASCII.GetString(fin.ReadBytes((int)lNameLength));
                var tinLength = fin.ReadUInt32();
                entity.TIN = Encoding.ASCII.GetString(fin.ReadBytes((int)tinLength));
                entity.EntityId = fin.ReadUInt16();
                var entityNodeCount = fin.ReadUInt32();

                var entityAttributeCount = fin.ReadByte();

                // fin.BaseStream.Seek(f.Position - entityAttributeCount, SeekOrigin.Begin);

                while (entityAttributeCount >= 255)
                {
                    Byte[] len = new Byte[1];

                    len[0] = (byte)255;

                    fin.ReadByte();
                    entityAttributeCount -= 255;
                }

                var serviceIdOrType = fin.ReadUInt16();
                entity.EntityId = fin.ReadUInt16();

                var aatableIdentity = fin.ReadUInt16();
                var jurisNodeCount = fin.ReadUInt32();

                for (int i = 0; i < jurisNodeCount; i++)
                {
                    Jurisdiction jur = new Jurisdiction();

                    var jurisAttributeCount = fin.ReadByte();

                    while (jurisAttributeCount >= 255)
                    {
                        Byte[] len = new Byte[1];

                        len[0] = (byte)255;

                        fin.ReadByte();
                        jurisAttributeCount -= 255;
                    }

                    var serviceIdOrTypejuris = fin.ReadUInt16();
                    jur.JurisdictionId = fin.ReadUInt16();

                    var tableId = fin.ReadUInt16();
                    var rowCount = fin.ReadUInt32();

                    jur.DDTime.AddSeconds((double)fin.ReadUInt32());
                    var abbrLength = fin.ReadUInt32();
                    jur.Abbr = Encoding.ASCII.GetString(fin.ReadBytes((int)abbrLength));

                    jur.TaxData = new TaxData();

                    jur.TaxData.Schema = (ushort)fin.ReadUInt32();
                    jur.TaxData.NextRowId = fin.ReadUInt32();
                    jur.TaxData.Timesaved = (long)fin.ReadUInt32();
                    jur.TaxData.CalcMode = (ushort)fin.ReadUInt32();
                    jur.TaxData.Calcneeded = (ushort)fin.ReadUInt32();
                    var tfdTableDicCount = fin.ReadUInt16();

                    
                    for (int j = 0; j < tfdTableDicCount; j++)
                    {
                        var tableKey = fin.ReadUInt16();
                        var version = fin.ReadUInt16(); // 102
                        var ddTimeStamp = fin.ReadUInt32(); // 1323196621
                        var locationCount = fin.ReadUInt16();

                        jur.TaxData.TFD = new TaxVBFormulaeDataItem();
                        jur.TaxData.TFD.tableDict = new Dictionary<int, Dictionary<int, Dictionary<int, Dictionary<int, object>>>>();

                        for (int k = 0; k < locationCount; k++)
                        {
                            var locationId = fin.ReadUInt16();
                            var locationRowCount = fin.ReadUInt16(); // 2
                            var rowId = fin.ReadUInt32();
                            var rowksValue = fin.ReadUInt32();
                            var logicFormCount = fin.ReadUInt16();

                            for (int l = 0; l < logicFormCount; l++)
                            {
                                var logicFormKey = fin.ReadUInt16();

                                var dataItemsCount = fin.ReadUInt16();

                                for (int m = 0; m < dataItemsCount; m++)
                                {
                                    var dataItemKey = fin.ReadUInt16();
                                    var dataItemPropertyCount = fin.ReadByte();
                                    var dlFlags = fin.ReadUInt16(); // 4096

                                    for (int n = 0; n < dataItemPropertyCount; n++)
                                    {
                                        var dataItemPropertyFlag = fin.ReadUInt16();

                                        if(fin.GetType() is UInt32)
                                        {
                                            var dataItemValue = fin.ReadUInt32();
                                        }
                                        else
                                        {
                                            var dataItemValueLength = fin.ReadUInt32();
                                            var dataItemValue = Encoding.ASCII.GetString(fin.ReadBytes((int)dataItemValueLength));
                                        }
                                    }
                                }
                            }
                        }
                    }
                    
                    jurisdiction.Add(jur);
                }

                entity.Jurisdictions = jurisdiction;
                listEntity.Add(entity);
            }

            taxReturn.Entities = listEntity;

            fin.Close();
            f.Close();

            return true;
        }



Attached File  file.zip (236bytes)
Number of downloads: 13
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2