3

I have a incoming stream of bytes (unsigned char) from either a file or network. I need this data placed in a class, and is looking for a NET-way of doing this.

I bet some does this all the time, so I guess there is a better method to do this than using BitConverter.

I realize I supplied too litle information. Let me try with an example class:

class data { 
void doSmething(); 
int var1; 
float var2; 
} 

Then I want to transfer the data (var1 and var2) contained in this class over f.ex. a network socket and receive the data on the other end

rozon
  • 2,518
  • 4
  • 23
  • 37

4 Answers4

3

As Jon mentioned, it's not clear, what you need. Maybe you are talking about maybe it is Binary serialization what you are looking for?

PiRX
  • 3,515
  • 1
  • 19
  • 18
1

It's not entirely clear what you mean, but if you're basically looking for a way to buffer the data so you can get at it later, MemoryStream is probably your best bet. Write all your data to it, then set Position to 0 and you can read the data back again.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I realize I supplied too litle information. Let me try with an example class: class data { void doSmething(); int var1; float var2; } Then I want to transfer the data (var1 and var2) contained in this class over f.ex. a network socket and receive the data on the other end. – rozon Nov 24 '08 at 09:50
  • Right. You need a serialization framework, basically. There are lots of options here. For a cross-platform approach, I can recommend (with some bias) Google Protocol Buffers. Other alternatives include .NET's own binary serialization protocol, or XML serialization. – Jon Skeet Nov 24 '08 at 10:51
1

You have 2 options or Binary Serialization (as PiRX said) or XML Serialization, for performance, the better is binary, but i prefer xml serialization for its readibility:

 [Serializable]
    [XmlRoot("CONFIGURATION")]
    public class Configuration
    {
        EnterpriseCollection enterprises;
        public Configuration()
        {

            enterprises= new EnterpriseCollection();
        }
        [XmlElement("ENTERPRISE")]
        public EnterpriseCollection Enterprises
        {
            get
            {
                return this.enterprises;
            }
            set
            {
                this.enterprises = value;
            }
        }
        private string name;
        [XmlElement("NAME")]
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
netadictos
  • 7,602
  • 2
  • 42
  • 69
0

you can use the std::string class. One of its constructors takes char* as an argument so you can go straight from char* to string. and string is a great way of storing your character strings. go to http://www.cppreference.com/ for more information on about strings

user20844
  • 6,527
  • 4
  • 18
  • 9