Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Help reading a binary file

Help reading a binary file

Scheduled Pinned Locked Moved C#
data-structureshelpquestion
14 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David Williams
    wrote on last edited by
    #1

    I need to read a binary file into a ushort array. The file was created with another app, and just consists of two bytes per ushort (big endian). FileStream.Read will get the bytes in OK - as bytes. I'd like to read them in directly to ushort. Sounds simple. Is it?

    L P E 3 Replies Last reply
    0
    • D David Williams

      I need to read a binary file into a ushort array. The file was created with another app, and just consists of two bytes per ushort (big endian). FileStream.Read will get the bytes in OK - as bytes. I'd like to read them in directly to ushort. Sounds simple. Is it?

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      David Williams wrote: I'd like to read them in directly to ushort. Sounds simple. Is it? It does sound simple, especially if you see how a mapping of the will look. Unfortunately there is no direct way. I would make a class deriving from FileStream and overload the Read method with some thing like this:

      public ushort ReadUShort()
      {
      return (ushort) ((ReadByte() << 8) + ReadByte()); //not sure about my bit operations also order :zzz: CHECK IT!
      }

      public int Read( ushort[] buffer, int offset, int length)
      {
      Position += offset;
      int i = 0;
      if (CanRead) {
      for (; i < length; i++)
      {
      buffer[i] = ReadUShort();
      }
      }
      return i;
      }

      You can try that, someone might have an easier idea :~ Hope this helps :) Give them a chance! Do it for the kittens, dear God, the kittens! As seen on MS File Transfer: Please enter an integer between 1 and 2.

      D L 2 Replies Last reply
      0
      • D David Williams

        I need to read a binary file into a ushort array. The file was created with another app, and just consists of two bytes per ushort (big endian). FileStream.Read will get the bytes in OK - as bytes. I'd like to read them in directly to ushort. Sounds simple. Is it?

        P Offline
        P Offline
        Paul Riley
        wrote on last edited by
        #3

        Check out BinaryFormatter.Deserialize I'm pretty sure that will do the job for you. Paul

        J 1 Reply Last reply
        0
        • P Paul Riley

          Check out BinaryFormatter.Deserialize I'm pretty sure that will do the job for you. Paul

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          The formatters are used for serializing and deserializing classes/structures to and from streams. They also write and read an extra header that tells the runtime what type of object is in the stream and what name (if any) it has. To sum it up; the formatter can't be used unless it was used to create the data.

          James Sig code stolen from David Wulff

          P 1 Reply Last reply
          0
          • J James T Johnson

            The formatters are used for serializing and deserializing classes/structures to and from streams. They also write and read an extra header that tells the runtime what type of object is in the stream and what name (if any) it has. To sum it up; the formatter can't be used unless it was used to create the data.

            James Sig code stolen from David Wulff

            P Offline
            P Offline
            Paul Riley
            wrote on last edited by
            #5

            Daaamn! That's very cool... but slightly annoying if it's not what you want :laugh: Paul

            D 1 Reply Last reply
            0
            • L leppie

              David Williams wrote: I'd like to read them in directly to ushort. Sounds simple. Is it? It does sound simple, especially if you see how a mapping of the will look. Unfortunately there is no direct way. I would make a class deriving from FileStream and overload the Read method with some thing like this:

              public ushort ReadUShort()
              {
              return (ushort) ((ReadByte() << 8) + ReadByte()); //not sure about my bit operations also order :zzz: CHECK IT!
              }

              public int Read( ushort[] buffer, int offset, int length)
              {
              Position += offset;
              int i = 0;
              if (CanRead) {
              for (; i < length; i++)
              {
              buffer[i] = ReadUShort();
              }
              }
              return i;
              }

              You can try that, someone might have an easier idea :~ Hope this helps :) Give them a chance! Do it for the kittens, dear God, the kittens! As seen on MS File Transfer: Please enter an integer between 1 and 2.

              D Offline
              D Offline
              David Williams
              wrote on last edited by
              #6

              Yep. This works, and you do have the bit ops correct. Thanks:)

              L 1 Reply Last reply
              0
              • P Paul Riley

                Daaamn! That's very cool... but slightly annoying if it's not what you want :laugh: Paul

                D Offline
                D Offline
                David Williams
                wrote on last edited by
                #7

                Yeah, I tried the deserialize and found it needed a header. Just to see what it needed, I serialized a 10 byte binary file and found the binary formatter put 28 bytes of header up front. Oh, well. lippie's solution works. Thanks for the response, though.;)

                1 Reply Last reply
                0
                • D David Williams

                  Yep. This works, and you do have the bit ops correct. Thanks:)

                  L Offline
                  L Offline
                  leppie
                  wrote on last edited by
                  #8

                  David Williams wrote: Yep. This works, and you do have the bit ops correct. Thanks Cool :) I remembered I saw something like << 16 for Int32 , so the guess was made accordingly :wtf: hehe. However, the way I did it may not be (and probably is not) efficient. You maybe use the Encoding class (UTF8 and Unicode (BigEndian) ) in a reverse fashion, it sounds a lot like you are dealing with a UTF8 to Unicode(BigEndian) conversion. Alternatively, the more "dangerous" way is to use a bit of marshalling, remember what I said about the way the data is laid out in memory. So make a small converter.

                  ushort[] ConvertToUShortArray( byte[] array)
                  {
                  int size = Marshal.SizeOf(typeof(byte)) * array.Length;
                  ushort[] output = new ushort[array.Length/2];
                  IntPtr ptr = Marshal.AllocHGlobal(size);
                  Marshal.Copy(array, 0, ptr, array.Length);
                  Marshal.PtrToStructure(ptr, output); //hope this works ??
                  Marshal.FreeHGlobal(ptr);
                  return (ushort[])output;
                  }

                  Ugly!! but efficient :) Hope this helps :) Give them a chance! Do it for the kittens, dear God, the kittens! As seen on MS File Transfer: Please enter an integer between 1 and 2.

                  1 Reply Last reply
                  0
                  • D David Williams

                    I need to read a binary file into a ushort array. The file was created with another app, and just consists of two bytes per ushort (big endian). FileStream.Read will get the bytes in OK - as bytes. I'd like to read them in directly to ushort. Sounds simple. Is it?

                    E Offline
                    E Offline
                    Eric Gunnerson msft
                    wrote on last edited by
                    #9

                    I think the easiest thing to do is to use a BinaryReader, and just call ReadUInt16().

                    L D 2 Replies Last reply
                    0
                    • E Eric Gunnerson msft

                      I think the easiest thing to do is to use a BinaryReader, and just call ReadUInt16().

                      L Offline
                      L Offline
                      leppie
                      wrote on last edited by
                      #10

                      So it is simple :) Give them a chance! Do it for the kittens, dear God, the kittens! As seen on MS File Transfer: Please enter an integer between 1 and 2.

                      D 1 Reply Last reply
                      0
                      • L leppie

                        So it is simple :) Give them a chance! Do it for the kittens, dear God, the kittens! As seen on MS File Transfer: Please enter an integer between 1 and 2.

                        D Offline
                        D Offline
                        David Williams
                        wrote on last edited by
                        #11

                        No, not simple. Gunnerson's BinaryReader works, but for little endian, not big. :((

                        1 Reply Last reply
                        0
                        • E Eric Gunnerson msft

                          I think the easiest thing to do is to use a BinaryReader, and just call ReadUInt16().

                          D Offline
                          D Offline
                          David Williams
                          wrote on last edited by
                          #12

                          This works for little endian. Will it work for big endian if I call it thus: 16IntURead() ;)

                          E 1 Reply Last reply
                          0
                          • D David Williams

                            This works for little endian. Will it work for big endian if I call it thus: 16IntURead() ;)

                            E Offline
                            E Offline
                            Eric Gunnerson msft
                            wrote on last edited by
                            #13

                            I think the answer to that would be "no". You'd need to call ReadUInt16FlipFlopBytes()... If you need to do this, you can either do the byte flip yourself, or you may be able to use the IPAddress class.

                            1 Reply Last reply
                            0
                            • L leppie

                              David Williams wrote: I'd like to read them in directly to ushort. Sounds simple. Is it? It does sound simple, especially if you see how a mapping of the will look. Unfortunately there is no direct way. I would make a class deriving from FileStream and overload the Read method with some thing like this:

                              public ushort ReadUShort()
                              {
                              return (ushort) ((ReadByte() << 8) + ReadByte()); //not sure about my bit operations also order :zzz: CHECK IT!
                              }

                              public int Read( ushort[] buffer, int offset, int length)
                              {
                              Position += offset;
                              int i = 0;
                              if (CanRead) {
                              for (; i < length; i++)
                              {
                              buffer[i] = ReadUShort();
                              }
                              }
                              return i;
                              }

                              You can try that, someone might have an easier idea :~ Hope this helps :) Give them a chance! Do it for the kittens, dear God, the kittens! As seen on MS File Transfer: Please enter an integer between 1 and 2.

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              I have big problem for making class deriving from FileStream or BinaryReader I need to read a binary file into a some struct. Pleas help.

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups