Use hex representation of ints, it reduces size of the string:
Serialize:
List<Point> list = new List<Point>(new Point[] {new Point(1, 2), new Point(10, 20), new Point (100, 200), new Point(1000, 2000), new Point(10000, 20000)});
// 1. To.
StringBuilder sb = new StringBuilder();
foreach (Point point in list)
{
sb.Append(Convert.ToString(point.X, 16));sb.Append('.');
sb.Append(Convert.ToString(point.Y, 16));sb.Append(':');
}
string serialized = sb.ToString();
Here is the string in form: "x.y:1.2:a.14:64.c8:3e8.7d0:2710.4e20:"
Deserialize, splitting ('serialized' is the string contains chain of numbers):
string[] groups = serialized.Split(new char[] {':'}, StringSplitOptions.RemoveEmptyEntries);
foreach (string group in groups)
{
string[] coords = group.Split('.');
restored.Add(new Point(Convert.ToInt32(coords[0], 16), Convert.ToInt32(coords[1], 16)));
}
Or you could you regexp to parse groups("[0-9a-fA-F].[0-9a-fA-F]"), it's up to you. I am not sure which is quicker.
Or a simple state machine (just for fun):
List<Point> restored = new List<Point>();
string value = default(string);
int left = 0;
int x = 0, y = 0;
for (int i = 0; i < serialized.Length; i++)
{
if (serialized[i] == '.')
{
value = serialized.Substring(left, i - left);
left = i + 1;
x = Convert.ToInt32(value, 16);
}
else if (serialized[i] == ':')
{
value = serialized.Substring(left, i - left);
left = i + 1;
y = Convert.ToInt32(value, 16);
restored.Add(new Point(x, y));
}
}
IMHO.
EDITED: Or even better to pack integers to groups of hex: 1212 to 'CC' like it is used in old financial systems; it makes length of string two times less.