Binary to other data types

How to convert a binary file to readable and usable data

Page 1 of 1

1 Replies - 1017 Views - Last Post: 18 February 2010 - 09:36 AM Rate Topic: -----

#1 andrelom   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-February 10

Binary to other data types

Posted 18 February 2010 - 02:41 AM

Good day, I'm new here and would like some advise on the convertion of data retrieved from a binary file to readable/usable data. So let me explain:

I recieve a file from some of our developers that is in binary format, within this file is a bunch of records in a specific format/structure. they also provide me with a structure for the information written to the binary file (this structure is in C), for example:

typedef   struct myStructure
{
   unsigned short valueOne;
   char valueTwo[16];
   long code;             
   long date;  
   long someOtherValue
   unsigned short aShortValue;
}



Ok so now obviously I have to make use of this structure to convert the binary data within the file, I'm not exactly sure how to do this, I will probably have to read how many ever bytes into a byte array for the first element specified in the struct then convert this to a INT making use of System.Convert??

As I said I'm not sure of how to do this at all, can any one please give me some advise.

Thank you
Andre

Is This A Good Question/Topic? 0
  • +

Replies To: Binary to other data types

#2 MentalFloss   User is offline

  • .
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,590
  • Joined: 02-September 09

Re: Binary to other data types

Posted 18 February 2010 - 09:36 AM

There isn't really enough information here to make any serious guesses as to how to implement this.
Based on what you have, you may want to make a class that mimics the structure the developers are using. With that, you can create a new loader class (or alternately create a static method on that class) and use that to load a binary piece of data.

public class Record
{
    public ushort ValueOne
    { get; set; }

    public string ValueTwo
    { get; set; }

    public long Code
    { get; set; }

    // This was long date but I don't think it's wise to do it like that.
    public DateTime Date
    { get; set; }

    public long someOtherValue
    { get; set; }

    public ushort ShortValue
    { get; set; }

    public static Record Load(long code)
    {
        // Based on the provided long, return a new record with the values populated from the developer's data.
        // I do make the assumption that code on this class is the identifier of the record by the way - if this is wrong, obviously this needs to change.
    }
}



So, you populate the records perhaps like this:

public class Program
{
    public static void Main(string[] args)
    {
        Record record = Record.Load(args[0]);
    }
}



Or whatever.

Now, the question of translating the actual binary information to an object. I'm betting this might help: http://www.yoda.arac...readbinary.html

Oh, and when it comes to that holder class, you may end up having to make it a packed structure. Here's how: http://www.vsj.co.uk...play.asp?id=501

There's a lot of information in that link so you may be covered there.

Further assessments of your problem is going to require more example code and/or a more detailed design specification.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1