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. Casting

Casting

Scheduled Pinned Locked Moved C#
data-structuresquestion
3 Posts 3 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.
  • A Offline
    A Offline
    apollo house
    wrote on last edited by
    #1

    I have a Stream of bytes from a file, I wish to cast the byte array into various data types eg int doubles etc( no I cannot do this at the file read stage due to the nature and format of the data), but under compilation I either get on the data from the first byte or complier warnings being 'unable to cast byte[] to int'. How can I convert an array of bytes to another data type eg a 2 byte array to an int. Thanks in advance

    S N 2 Replies Last reply
    0
    • A apollo house

      I have a Stream of bytes from a file, I wish to cast the byte array into various data types eg int doubles etc( no I cannot do this at the file read stage due to the nature and format of the data), but under compilation I either get on the data from the first byte or complier warnings being 'unable to cast byte[] to int'. How can I convert an array of bytes to another data type eg a 2 byte array to an int. Thanks in advance

      S Offline
      S Offline
      Stephane Rodriguez
      wrote on last edited by
      #2

      Conversion from a Stream should be a service provided by a Stream-derived class. Watchout MemoryStream, TextReader, StringReader, ... When your stream cursor has read 4 bytes, use the System.Convert helper class to get an Int32.

      1 Reply Last reply
      0
      • A apollo house

        I have a Stream of bytes from a file, I wish to cast the byte array into various data types eg int doubles etc( no I cannot do this at the file read stage due to the nature and format of the data), but under compilation I either get on the data from the first byte or complier warnings being 'unable to cast byte[] to int'. How can I convert an array of bytes to another data type eg a 2 byte array to an int. Thanks in advance

        N Offline
        N Offline
        Nathan Blomquist
        wrote on last edited by
        #3

        Checkout the MemoryStream and BinaryReader classes. The MemoryStream class allows you to wrap a byte array in a stream, and then BinaryReader class allows you to read primitives (doubles, floats, ints, etc...) from a stream. Hope this helps, Nathan --------------------------- Hmmm... what's a signature?

        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