'
Tutorial on Encoding orderobject into a string then Decode string back to original orderobject
Can someone assist me or point me to the right direction.
Random randomCreditCardNo = new Random();
string senderId = retailers[i].ManagedThreadId.ToString();
int cardNo = randomCreditCardNo.Next(5000, 7000);
int amount = chicken.getPrice();
OrderObject orderObject = new OrderObject(senderId, cardNo, amount);
Loading
VulpesPosted Oct 3, 2011, 10:41 AM
Note that, whilst this produces strings which are not human-readable (at least by casual inspection), it is not cryptographically secure. A much stronger cipher would need to be used to achieve that.
David SmithPosted Oct 2, 2011, 11:07 PM
VulpesPosted Oct 2, 2011, 4:57 AM
using System;
public class OrderObject
{
public string SenderId {get; private set;}
public int CardNo {get; private set;}
public int Amount {get; private set;}
public OrderObject(string senderId, int cardNo, int amount)
{
SenderId = senderId;
CardNo = cardNo;
Amount = amount;
}
public static string Encode(OrderObject oo)
{
return String.Format("{0},{1},{2}", oo.SenderId, oo.CardNo, oo.Amount);
}
public static OrderObject Decode(string s)
{
string[] items = s.Split(',');
return new OrderObject(items[0], int.Parse(items[1]), int.Parse(items[2]));
}
}
class Test
{
static void Main()
{
OrderObject oo = new OrderObject("5", 5200, 6);
string s = OrderObject.Encode(oo);
Console.WriteLine(s);
OrderObject oo2 = OrderObject.Decode(s);
Console.WriteLine("{0},{1},{2}", oo2.SenderId, oo2.CardNo, oo2.Amount);
Console.ReadKey();
}
}
If the string needs to be changed so that it is not human-readable, then there are any number of ways to do it depending on how much security is needed.
Notice that it's not possible to decode back to the original object, just a clone of it.