Wednesday, March 4, 2009

Encoding and Decoding string Values in Silverlight

Silverlight allows us to Encode and Decode string values which can be used when we pass the values between Javascript functions or between two xap files in the same browser.

To Encode a string use the following code

byte[] bt = Encoding.UTF8.GetBytes("This is a Encoded String");
string encodedData = Convert.ToBase64String(bt);

Where encodedData will be the Encoded string.


To decode the Encoded String use the following code

byte[] decbuff = Convert.FromBase64String(encodedData);
string decodedData = Encoding.UTF8.GetString(decbuff, 0, decbuff.Length);

Where
decodedData will be the Decoded string.