Casting
-
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
-
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
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.
-
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
Checkout the
MemoryStream
andBinaryReader
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?