JG Vimalan's Blog

It is all about coding!

Convert sByte [] to string

I got a task where I need to convert a compressed string in sbyte[] back to original string. The example string used here is “Hello World!!!Hello World!!!”

Here is the code,

sbyte[] sbyteData = new sbyte[] {
31, -117, 8, 0, 0, 0, 0, 0, 0, 0, -13, 72, -51, -55, -55, 87, 8, -49, 47, -54, 73, 81, 84, 84, -12, 64, -31, 1, 0, 111, 9, 127, -117, 28, 0, 0, 0 }; //compressed string
           
//Convert sbyte[] to byte[]
byte[] byteData = Array.ConvertAll(sbyteData, (a) => (byte)a);
System.IO.Stream stream = new System.IO.MemoryStream(byteData);

System.IO.Compression.GZipStream gstream = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Decompress, true);
byte[] resultBytes = new byte[byteData.Length];

//fill the resultBytes with decompressed byte array
int readResult = gstream.Read(resultBytes, 0, resultBytes.Length);

//you got the actual string here
string stringFromByteData = utf8Encoding.GetString(resultBytes);

Advertisements

February 22, 2011 - Posted by | C#.NET

1 Comment »

  1. thanks for sharing this useful information

    Comment by Yuvraj | February 25, 2011 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: