My favorites
▼
|
Sign in
jugad
This is a judag to use Google Code hosting to share various code with people,
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
C#
/
StructureToString
/
Program.cs
r9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
namespace StructureToString
{
class Program
{
struct Payload
{
public int a;
public int b;
}
struct Packet
{
public char a;
public int b;
public float c;
public Payload s;
}
static public string SerializeBuffer(object obj, ref int size)
{
size = Marshal.SizeOf(obj.GetType());
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(obj, ptr, true);
byte[] byteArray = new byte[size];
string encode_qp = "";
for (int i = 0; i < size; ++i)
{
byte b = (byte)Marshal.ReadByte(ptr, i);
encode_qp += "=" + b.ToString("X2");
}
Marshal.FreeHGlobal(ptr);
return encode_qp;
}
static public void GetDeSerializedBuffer(ref object Obj, ref string QPData)
{
int size = Marshal.SizeOf(Obj.GetType());
IntPtr ptr = Marshal.AllocHGlobal(size);
for (int i = 0; i < size; i++)
{
Marshal.WriteByte(ptr, i, ReadByteFromQPString(ref QPData));
}
Obj = Marshal.PtrToStructure(ptr, Obj.GetType());
Marshal.FreeHGlobal(ptr);
}
static byte GetByteValueFromChar(char c)
{
if (char.IsDigit(c))
{
return (byte)(c - '0');
}
else if (char.IsLetter(c))
{
char cc = char.ToUpper(c);
return (byte)(10 + cc - 'A');
}
else
{
return (byte)0;
}
}
private static byte GetByte(string bytestr)
{
if (bytestr.Length == 0)
{
return (byte)0;
}
if (bytestr.Length == 1)
{
return GetByteValueFromChar(bytestr[0]);
}
if (bytestr.Length >= 2)
{
return (byte)(16 * GetByteValueFromChar(bytestr[0]) + GetByteValueFromChar(bytestr[1]));
}
return 0;
}
static private byte ReadByteFromQPString(ref string qp_string)
{
string byte_str = "";
if (qp_string.Length == 0)
{
throw new Exception("byte can not be read");
}
if (qp_string[0] == '=')
{
while (true)
{
if (qp_string.Length > 1 && qp_string[1] != '=')
{
byte_str += qp_string[1];
}
else
{
break;
}
if (qp_string.Length > 2 && qp_string[2] != '=')
{
byte_str += qp_string[2];
}
break;
}
qp_string = qp_string.Substring(byte_str.Length + 1);
return GetByte(byte_str);
}
else
{
byte byte_val = (byte)qp_string[0];
qp_string = qp_string.Substring(1);
return byte_val;
}
}
public static void WriteToFile(string data)
{
StreamWriter wr = new StreamWriter("test.txt");
wr.Write(data);
wr.Close();
}
public static string ReadFromFile()
{
StreamReader rd = new StreamReader("test.txt");
string data=rd.ReadToEnd();
rd.Close();
return data;
}
static void Main(string[] args)
{
Packet bData1;
bData1.a = 'c';
bData1.b = 20;
bData1.c = 30f;
bData1.s.a = 10;
bData1.s.b = 20;
Packet bData2;
bData2.a = '\0';
bData2.b = 0;
bData2.c = 0f;
bData2.s.a = 0;
bData2.s.b = 0;
int size = 0;
string SerializedBuffer=SerializeBuffer(bData1, ref size);
Console.Out.WriteLine("size=" + size+"\n");
WriteToFile(SerializedBuffer);
string DataReadFromFile = ReadFromFile();
object obj = (object)bData2;
GetDeSerializedBuffer(ref obj, ref DataReadFromFile);
bData2 = (Packet)obj;
Console.Out.WriteLine("bData2.a=" + bData2.a + "\n");
Console.Out.WriteLine(" bData2.b=" + bData2.b + "\n");
Console.Out.WriteLine("bData2.c=" + bData2.c + "\n");
Console.Out.WriteLine("bData2.s.a=" + bData2.s.a + "\n");
Console.Out.WriteLine("bData2.s.b=" + bData2.s.b + "\n");
}
}
}
Show details
Hide details
Change log
r2
by mark.dawn on Jul 2, 2009
Diff
[No log message]
Go to:
/trunk/C#
/trunk/C#/StructureToString
.../C#/StructureToString/Program.cs
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 5215 bytes, 166 lines
View raw file
Powered by
Google Project Hosting