473,503 Members | 7,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Struct Deserialization Variable Array Length Inside Struct

1 New Member
Expand|Select|Wrap|Line Numbers
  1. [StructLayou(LayoutKind = Sequential,Pack=1,Size = 18)]
  2. public struct Character_Items
  3. {
  4. public short Armor;
  5. public short Helm;
  6. public short WeaponL;
  7. public short WeaponR;
  8. public short Pants;
  9. public short Boots;
  10. public short Gloves;
  11. public short Levels;
  12. public byte Glow;
  13. public byte Guardian;
  14. }
  15.  
  16. [StructLayou(LayoutKind = Sequential,Pack=1,Size = 34)]
  17. public struct Character_Block
  18. {
  19. public byte Position;
  20. [MarshalAs(UnmanagedType.ByValArray,SizeConst = 10)]
  21. public short Level;
  22. public byte Class;
  23. public byte CtrlCode;
  24. public Character_Items Items;
  25. public byte GuildStatus;
  26. }
  27.  
  28. So here I have a problem when deserealize
  29. [StructLayou(LayoutKind = Sequential,Pack=1)]
  30. public struct Character_List
  31. {
  32. public byte Head;
  33. public byte Length;
  34. public byte Code;
  35. public byte OpCode;
  36. public byte SubCode;
  37. public byte Separator;
  38. public byte Count;
  39. //Here the problem i dont know how to set the MarshalAs
  40. [MarshalAs(UnmanagedType.ByValArray)]
  41. public Character_Block[] Characters
  42.  
  43. public explicit operator Character_List(byte[] Value)
  44. {
  45. return Deserialize<Character_List>(Value);
  46. }
  47.  
  48. }
  49.  
  50. //I use it for Deserialize
  51. public static T Deserialize<T>(byte[] Data)
  52. {
  53. IntPtr TmpData = Marshal.AllocHGlobal(Data.Length);
  54. Marshal.Copy(Data, 0, TmpData, Data.Length);
  55. T TmpStruct = Marshal.PtrToStructure(TmpData, typeof(T));
  56. Marshal.FreeHGlobal(TmpData);
  57. return TmpStruct;
  58. }
  59.  
I receive the data from server via socket and on receive if case of Character List opCode I do
Expand|Select|Wrap|Line Numbers
  1. OnCharListRecv((Character_List)Data));
  2.  
Data is splitted and it have only the packet length
because length of packet is set in Data[1]

But the number of characters is variable
its set on Count inside struct or same as Data[6]

Ill need set the max number of characters thats 5?
Or there is a way to set a variable array size?

Thanks, and sorry for bad english/code ;D hehe
Aug 8 '10 #1
0 1337

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3950
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
6
5419
by: Stuart Norris | last post by:
Dear Readers, I am attempting to initialise a struct contiaing a dynamic character string. In the example below I am trying to initialise the name field so that my struct does not waste space. ...
19
3055
by: Geetesh | last post by:
Recently i saw a code in which there was a structer defination similar as bellow: struct foo { int dummy1; int dummy2; int last }; In application the above array is always allocated at...
9
1731
by: portroe | last post by:
by the way, how do you deal with a variable array lenth, for example if the array depends on the number of years 'input' by the user, Dim Years(?) As Integer thanks
16
16360
by: Gerrit | last post by:
Hello, Is it possible to sort an array with a struct in it? example: I have a struct: public struct mp3file { public int tracknr;
5
4310
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
26
2528
by: alberto | last post by:
Hi. Im newbie in C language. I have a binary file with many character arrays of 50 character defined as char array But in some cases, many of these 50 characters are not being used. I would...
3
1550
by: chrismcb | last post by:
In C++ I want to statically create an array that contains variable arrays inside of it. Or something similar. Essentially I want to do something like this: { {"name1", {a, b, c} }, {"name2",...
9
6498
by: AM | last post by:
Hi, I have a C++ Dll that has a function that is being exported as shown below extern "C" __declspec(dllexport) validationResult __stdcall _validateData(double dataToMat, int time); A...
4
8087
by: robert.waters | last post by:
I have to parse a binary file having a number of fixed records, each record containing datas in fixed positions. I would like to parse this binary file into an array of structures having members...
0
7207
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
7357
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...
1
7012
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
5598
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
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.