JG Vimalan's Blog

Sharing is caring :)

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);

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 comment