473,407 Members | 2,629 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,407 software developers and data experts.

struct to byte[] conversion

Hi all,
1. if I use BinaryFormatter(for the struct below) I get:

 ˙˙˙˙  IMsgProtocol, Version=1.0.1291.23445, Culture=neutral,
PublicKeyToken=null msgProtocol.Login uinhashstatus description
msgProtocol.eStatus  Y ý˙˙˙msgProtocol.eStatus
value__    asdasda
2. is the easier way to convert Header struct to byte[]
and back just like record in C
(somthing like BinaryFormatter but without this all
useless (for me) things)
public enum eStatus
{
NotAvail = 0x0001,
Avail = 0x0002,
Busy = 0x0003,
Invisible = 0x0014,
}

public struct Login
{
public Login(byte[] inbyte)
{
uin = BitConverter.ToInt32(inbyte,0);
hash = BitConverter.ToInt32(inbyte, 4);
status = (eStatus)BitConverter.ToInt32(inbyte, 2 * 4);
description = Encoding.ASCII.GetString(inbyte,3*4,inbyte.Length - 3 *
4);// BitConverter.ToString(inbyte, 3 * 4);
}
public int uin;
public int hash;
public eStatus status;
public string description;
public byte[] GetBytes()
{
byte[] ret;
MemoryStream ms = new MemoryStream();
ret = BitConverter.GetBytes(uin);
ms.Write(ret,0,ret.Length);
ret = BitConverter.GetBytes(hash);
ms.Write(ret,0,ret.Length);
ret = BitConverter.GetBytes((int)status);
ms.Write(ret,0,ret.Length);
ret = Encoding.ASCII.GetBytes(description);
ms.Write(ret,0,ret.Length);
return ms.ToArray();
}
}

--
Selvin - Przemek Sulikowski
se****@osadkowski.com.pl
Nov 13 '05 #1
0 2268

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

Similar topics

8
by: Grant Edwards | last post by:
Perhaps I'm doing something wrong: the struct module docs say it's IEE 754, but I can't figure out how to get it to handle NaN values correctly (either packing or unpacking). >>> x =...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
3
by: Pablo Gutierrez | last post by:
I have a C# method that reads Binary data (BLOB type) from a database and returns the data an array of bytes (i.e byte outbyte = new byte;). The BLOB column is saved into the database by a C...
15
by: Ken Allen | last post by:
I have some code from C/C++ that I am attempting to port to C#. I have come across an interesting problem that is quite common in complex C/C++ code: the us of UNION in structure definitions to...
28
by: Tamir Khason | last post by:
Follwing the struct: public struct TpSomeMsgRep { public uint SomeId;
3
by: VMI | last post by:
How can I convert the following C struct into a C# struct? The struct is sent to an API function (written in C) as parameter, and the function fills it with data. I tried to convert it and most of...
4
by: taskswap | last post by:
I'm converting an application that relies heavily on a binary network protocol. Within this protocol are a lot of byte arrays of character data, like: public unsafe struct MsgAddEntry {...
6
by: Jack | last post by:
Is it possible to cast a 4-byte data type (e.g. an unsigned int) to a 4-byte struct? Sometimes the HIWORD and LOWORD of a 4-byte value contain information independent of each other. Is there a...
8
by: sexauthor | last post by:
I'm converting a VB6 application over that called a 3rd party DLL with specific data structures. The VB6 code defined custom types for those data structures (ie: one with the specific data types,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.