struct to byte[] conversion

S

Selvin

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();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top