I have to parse a packet which is stored in byte[] in ascii code for example byte[] user = new byte[] {112, 114, 97, 116, 121, 117, 115, 104, 0, 0, 0, 0, 49, 50, 51, 0} where first 12 bytes are servername and last four bytes are some id.
By going through previous stackoverflow posts I came up with this code
[StructLayout(LayoutKind.Explicit)]
struct packetrf
{
public ulong servername
{
get
{
return (ulong)servername1 | ((ulong)servername2 << 8) | ((ulong)servername3 << 16) | ((ulong)servername4 << 24) | ((ulong)servername5 << 32) | ((ulong)servername6 << 40) | ((ulong)servername7 << 48) | ((ulong)servername8 << 56) | ((ulong)servername9 << 64) | ((ulong)servername10 << 72) | ((ulong)servername11 << 80) | ((ulong)servername12 << 88);
}
}
[FieldOffset(0)]
public byte servername1;
[FieldOffset(1)]
public byte servername2;
[FieldOffset(2)]
public byte servername3;
[FieldOffset(3)]
public byte servername4;
[FieldOffset(4)]
public byte servername5;
[FieldOffset(5)]
public byte servername6;
[FieldOffset(6)]
public byte servername7;
[FieldOffset(7)]
public byte servername8;
[FieldOffset(8)]
public byte servername9;
[FieldOffset(9)]
public byte servername10;
[FieldOffset(10)]
public byte servername11;
[FieldOffset(11)]
public byte servername12;
[FieldOffset(12)]
public Int32 imei_msn;
};
static private pack fromByte(byte[] arr)
{
pack str = new pack();
GCHandle handle = GCHandle.Alloc(arr, GCHandleType.Pinned);
str = (pack)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(pack));
handle.Free();
return str;
}
static private void data_parser(byte[] pkt, int size, int indexno)
{
packetrf data = fromByte(pkt);
Console.WriteLine();
Console.WriteLine(data.servername);
Console.WriteLine(data.imei_msn);
Console.ReadKey();
}
public static void Main()
{
byte[] user = new byte[] {112, 114, 97, 116, 121, 117, 115, 104, 0, 0, 0, 0, 49, 50, 51, 0}
data_parser(user, 1, 2);
}
When I run this code my output is 7526488566770266736 for servername and 13106 for imei_msn which is probably is probably ascii->hex->decimal format. Now the problem I am facing is that how can I convert these numbers to some meaning full information like first one to string showing name and second one to imei_msn in decimal format?