473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to convert a class/struct to a byte array?

Hi,

Assuming I have a class/struct with only following types
float, int, string

what is the most efficient way to pack it to a single byte array before I
send it out. I tried to use Bitconverter.GetBytes, but not sure what is the
best/quickest way to combine all bytes together.

Thanks

Chris
Oct 7 '08 #1
1 2379
On Tue, 07 Oct 2008 11:30:01 -0700, chrisben
<ch******@discussions.microsoft.comwrote:
Hi,

Assuming I have a class/struct with only following types
float, int, string

what is the most efficient way to pack it to a single byte array before I
send it out. I tried to use Bitconverter.GetBytes, but not sure what is
the
best/quickest way to combine all bytes together.
One way to do it would be to take the byte arrays as they are converted
and write them to a MemoryStream instance.

However, if you're doing stream-oriented output, you might find the
BinaryWriter and BinaryReader (for the other end) more appropriate. You
could use the BinaryWriter to write to a MemoryStream instance which you
then convert to a byte array after all the data's been read and send those
bytes, or if you're dealing with some sort of stream in the first place,
just write directly to the stream with the BinaryWriter and skip the
MemoryStream altogether.

Pete
Oct 7 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Graham Nicholls | last post by:
Hi, I'm trying to size a jpeg file. The file size is held in a short (2 byte integer) at a certain offset. Once I've found these two bytes (they're in MSB,LSB order), I need to convert them to...
7
by: Prabhu | last post by:
Hi, I have to send a structure through TCPClient socket. we can send only byte array through the socket, So please any one can help me by telling How to convert a struct object into an byte...
2
by: Mel WEaver | last post by:
Hello, I have the following delphi structure for a binary file. I'm looking for idea how to read this file. Mel type TMenuDataStruct = packed record exename : string;
5
by: Amil Hanish | last post by:
I have a huge byte array from another API that contains structs of differing sizes. I'd like to create n structs based on data in the byte array. But how? For example, if I want to take bytes...
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
9
by: Charles Law | last post by:
Suppose I have a structure Private Structure MyStruct Dim el1 As Byte Dim el2 As Int16 Dim el3 As Byte End Structure I want to convert this into a byte array where
0
by: ambeer | last post by:
Hello, i have created the struct: #region PacketStructures struct MainPacket { HeaderData headerData; public byte moduleType; ...
1
by: Paul Jarvis | last post by:
I have a large structure, below is a simplistic version of my structure: public struct MeanMinMaxSd { public double mean; public double min; public double max; public double sd; }
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.