struct to byte[] conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Selvin

    #1

    struct to byte[] conversion

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

     ÿÿÿÿ  IMsgProtocol, Version=1.0.129 1.23445, Culture=neutral ,
    PublicKeyToken= null msgProtocol.Lo gin uinhashstatu s description
    msgProtoco l.eStatus  Y ýÿÿÿmsgProtoc ol.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.To Int32(inbyte,0) ;
    hash = BitConverter.To Int32(inbyte, 4);
    status = (eStatus)BitCon verter.ToInt32( inbyte, 2 * 4);
    description = Encoding.ASCII. GetString(inbyt e,3*4,inbyte.Le ngth - 3 *
    4);// BitConverter.To String(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.Ge tBytes(uin);
    ms.Write(ret,0, ret.Length);
    ret = BitConverter.Ge tBytes(hash);
    ms.Write(ret,0, ret.Length);
    ret = BitConverter.Ge tBytes((int)sta tus);
    ms.Write(ret,0, ret.Length);
    ret = Encoding.ASCII. GetBytes(descri ption);
    ms.Write(ret,0, ret.Length);
    return ms.ToArray();
    }
    }

    --
    Selvin - Przemek Sulikowski
    selvin@osadkows ki.com.pl


Working...