473,569 Members | 2,593 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 1350

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

Similar topics

1
3960
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 bound inside struct or class to become global functions. It makes easier for me to use "struct.memberfunction()" instead of "globalfunction()"...
6
5422
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. I know if I change char name this will work, but I will waste alot of space. (I am learning so I want to learn the best way) How can I define a...
19
3061
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 runtime using malloc.In this last member of the structer "int last" is not
9
1737
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
16376
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
4319
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 that there is no difference between giving this function a
26
2545
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 like to know how could I know how many characters are really being used in each array ? Thanks
3
1556
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", {b, c} }, {"name2", {d} } } I want an array of names, and with each name I want to associate an list of
9
6511
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 structure is defined in the header(.h file) as shown below struct validationResult {
4
8095
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 that represent those fields, so that I can access the records in a meaningful way. Using C, I would have defined a struct that I could cast a byte...
0
7700
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7676
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
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 we have to send another system
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.