Results 1 to 5 of 5

Thread: Reading 2 bytes together

  1. #1

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Reading 2 bytes together

    Hi,

    I am reading a file into byte array and consists of header information.

    The format specs say that the first 2 bytes gives you a static value of 474. Right now I am converting the bytes (necessarily numeric values) into binary format (representing them as strings), append the first 2 bytes together and then convert it into integer again.

    That's working, but I just wanted to make sure if there is any other way to read more than 1 byte together and display the result.

    Or any other way, instead of reading into byte array, something long that line?

    Thank you
    Show Appreciation. Rate Posts.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reading 2 bytes together

    Code:
    byte[] data = File.ReadAllBytes("file path here");
    short header = BitConverter.ToInt16(data, 0);
    
    if (header == 474)
    {
        // Valid file.
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Reading 2 bytes together

    Thanks JMC, didn't knew about the BitConverter class.

    But, please correct me if I am wrong. How does BitConverter read the data off byte array? When I tried your code, it didn't give me the correct result. But when I swapped the bytes, it gave me correct result.

    csharp Code:
    1. byte[] data = System.IO.File.ReadAllBytes("rgbtest.jpg");
    2.  
    3.             byte[] nb = new byte[2];
    4.             nb[0] = data[1];
    5.             nb[1] = data[0];
    6.  
    7.             ushort magic = BitConverter.ToUInt16(nb, 0);
    8.  
    9. // swapping works
    10.             MessageBox.Show(string.Format("Magic Number: {0}", magic));
    I tried to read a JPG file too, whose first byte is FF, second byte is D8, so 2 bytes must be FFD8 and Decimal equivalent is 65496, and I get this only when I swap the values.

    What am I doing wrong?
    Show Appreciation. Rate Posts.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reading 2 bytes together

    I don't think you're doing anything wrong. I haven't really used the BitConverter all that much and I didn't take its endianness into account. It looks like it's little-endian, which makes sense, so you will, in fact, have to reverse the order of the bytes you want to test. Assuming .NET 3.5, LINQ can help you do that succinctly:
    csharp Code:
    1. byte[] data = File.ReadAllBytes("file path here");
    2. short header = BitConverter.ToInt16(data.Take(2).Reverse().ToArray(), 0);
    3.  
    4. if (header == 474)
    5. {
    6.     // Valid file.
    7. }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Hyperactive Member Arrow_Raider's Avatar
    Join Date
    Dec 2001
    Location
    AVR Lovers Club
    Posts
    423

    Re: Reading 2 bytes together

    This is how I would do it:

    csharp Code:
    1. string filename = "filename here";
    2. FileStream fs = File.OpenRead(filename);
    3. BinaryReader reader = new BinaryReader(fs);
    4. byte[] magicBytes = reader.ReadBytes(2);
    5.  
    6. //474 in big endian is 0x01DA
    7. if (magicBytes[0] == 0x01 && magicBytes[1] == 0xDA)
    8. {
    9.     //yay
    10. }

    Make sure you put this in the using directives:
    csharp Code:
    1. using System.IO;

    Of course, you also need to close the stream or the reader when you are done with it:
    csharp Code:
    1. reader.Close()
    Last edited by Arrow_Raider; Dec 29th, 2009 at 12:34 PM.
    My monkey wearing the fedora points and laughs at you.

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