Skip to main content
gamedev.net
🔒 Locked

Binary Data in C#

Started by Thevenin Dec 23, 2005 at 9:11 AM 3 replies 350+ views
Original Post
Thevenin
Thevenin
lol, abstraction~ I have an integer (int16) and I want to write its bytes to a byte[]. Inaddition, I have a byte[] and I want to read from it an int16. How do I do this in C#; without using unsafe code.
SiCrane
SiCrane
Did you try a MemoryStream with a BinaryReader/BinaryWriter?
Thevenin
Thevenin
Quote:
Original post by SiCrane
Did you try a MemoryStream with a BinaryReader/BinaryWriter?


MemoryStream yes, it didn't appear to have the functionality to do this conversion.

BinaryReader and Writer, no; but I'm currently googling them. [razz]

davepermen
davepermen
short ValueAsInt16 = .... ;byte[] ValueAsBytes = BitConverter.GetBytes(ValueAsInt16);byte[] ValueAsBytes = .... ;short ValueAsInt16 = BitConverter.ToInt16(ValueAsBytes, 0);


is this .NET 2.0 only or 1.1 as well? no clue, i'm working on vc# 2005..
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
Thevenin
Thevenin
        MemoryStream MyStreamer = new MemoryStream(this.TheClients[TheClient].TheBuffer);        BinaryReader MyBinaryReader = new BinaryReader(MyStreamer);


OIC, you gotta use MemoryStream with BinaryReader (Makes sence since BinaryReader takes in a stream, and MemoryStream makes a stream out of a byte array).

@davepermen
Neat, I'm checking that out. :P

Edit: I'm not sure if your question was at me or your code, but I'm working in VC#2005 aswell, so I'm using .NET 2.0.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.