472,371 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

string to byte[] back to string + Compression Failed!

I'm writing some code that will convert a regular string to a byte[]
for compression and then beable to convert that compressed string back
into original form.

Conceptually I have....

For compression
string ->(Unicode Conversion) byte[] -(Compression + Unicode
Conversion) string

For Decompression
string ->(Unicode Conversion) byte[] -(DECompression + Unicode
Conversion) string

The problem is that there's a code chunk that fails. Probably because
of some bad conversion somewhere in my code.

The key line that is constantly failing is....
int size = s.Read(write_data, 0, 8);
GZip algorithm gives me a ArrayIndexOutOfBounds
Deflate gives me some data corruption error. I looked at the byte[]
right after compression and right before decompression and they DO NOT
MATCH! What is the problem in this situation?

I need this process such that I get these 2 functions...

string CompressString(string in, Algorithm.GZip or Algorithm.Deflate);
string DecompressString(string in, Algorithm.GZip or
Algorithm.Deflate);

I've seen similar code but no potential fixes on Google.com
My Code is below....


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace Jeremyje.Utility
{
public class StringTransforms
{
public enum StringCompressionAlgorithm
{
GZip,
Deflate
}

public static byte[] UnicodeStringToByteArray(string str)
{
UnicodeEncoding enc = new UnicodeEncoding();
return enc.GetBytes(str);
}

public static string ByteArrayToUnicodeString(byte[] str_arr)
{
UnicodeEncoding enc = new UnicodeEncoding();
return enc.GetString(str_arr);
}

public static bool DecompressString(string in_string, out
string out_string)
{
return DecompressString(in_string, out out_string,
StringCompressionAlgorithm.GZip);
}

public static bool DecompressString(string in_string, out
string out_string, StringCompressionAlgorithm alg)
{
bool status = false;
out_string = in_string;

switch (alg)
{
case StringCompressionAlgorithm.GZip:
{
try
{
out_string = "";
int total_length = 0;
byte[] write_data = new byte[4096];
byte[] bData =
UnicodeStringToByteArray(in_string);

GZipStream s = new GZipStream(new
MemoryStream(bData), CompressionMode.Decompress);

while (true)
{
int size = s.Read(write_data, 0, 8);
if (size 0)
{
total_length += size;
out_string +=
Encoding.Unicode.GetString(write_data, 0, size);
}
else
{
break;
}
}
s.Close();
status = true;
}
catch (Exception e)
{
Console.WriteLine(e);
status = false;
}

return status;
}
case StringCompressionAlgorithm.Deflate:
{
try
{
out_string = "";
int total_length = 0;
byte[] write_data = new byte[4096];
byte[] bData =
UnicodeStringToByteArray(in_string);

DeflateStream s = new DeflateStream(new
MemoryStream(bData), CompressionMode.Decompress);

while (true)
{
int size = s.Read(write_data, 0, 8);
if (size 0)
{
total_length += size;
out_string +=
Encoding.Unicode.GetString(write_data, 0, size);
}
else
{
break;
}
}
s.Close();
status = true;
}
catch (Exception e)
{
Console.WriteLine(e);
status = false;
}

return status;
}

default:
break;
}

return status;
}

public static bool CompressString(string in_string, out string
out_string)
{
return CompressString(in_string, out out_string,
StringCompressionAlgorithm.GZip);
}

public static bool CompressString(string in_string, out string
out_string, StringCompressionAlgorithm alg)
{
bool status = false;
out_string = in_string;

switch(alg)
{
case StringCompressionAlgorithm.GZip:
{
try
{
MemoryStream ms = new MemoryStream();
Stream s = new GZipStream(ms,
CompressionMode.Compress);
byte[] bData =
UnicodeStringToByteArray(in_string);

s.Write(bData, 0, bData.Length);
s.Close();
byte[] compressed_data =
(byte[])ms.ToArray();
out_string =
ByteArrayToUnicodeString(compressed_data);
status = true;
}
catch(Exception e)
{
Console.WriteLine(e);
status = false;
}

return status;
}
case StringCompressionAlgorithm.Deflate:
{
try
{
MemoryStream ms = new MemoryStream();
Stream s = new DeflateStream(ms,
CompressionMode.Compress);
byte[] bData =
UnicodeStringToByteArray(in_string);

s.Write(bData, 0, bData.Length);
s.Close();
byte[] compressed_data =
(byte[])ms.ToArray();
out_string =
ByteArrayToUnicodeString(compressed_data);
status = true;
}
catch (Exception e)
{
Console.WriteLine(e);
status = false;
}

return status;
}

default:
break;
}

return false;
}
}
}

Feb 8 '07 #1
5 5842
Hi Jeremy,

Your problem is converting the compressed byte[] to string. After the
compression a string can't hold the data the byte[] holds and you lose
lots of data causing an exception when you try to decompress it.

Having Compress return a byte[] and Decompress take a byte[] will solve
your problem. If you need the byte[] to be represented as string you can
use Base64.

[DecompressString]
byte[] bData = Convert.FromBase64String(in_string);

[CompressString]
out_string = Convert.ToBase64String(compressed_data);
--
Happy Coding!
Morten Wennevik [C# MVP]
Feb 8 '07 #2
<je******@gmail.comwrote:
I'm writing some code that will convert a regular string to a byte[]
for compression and then beable to convert that compressed string back
into original form.
Don't try to encode arbitrary binary data as a string directly using
Encoding.Unicode. As Morten suggested, use Base64 instead.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 8 '07 #3
On Feb 8, 1:55 am, Jon Skeet [C# MVP] <s...@pobox.comwrote:
<jerem...@gmail.comwrote:
I'm writing some code that will convert a regular string to a byte[]
for compression and then beable to convert that compressed string back
into original form.

Don't try to encode arbitrary binary data as a string directly using
Encoding.Unicode. As Morten suggested, use Base64 instead.

--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Yeah, that's what I ended up doing but that's not much of a
compression since base64 makes the data a lot larger. Is there another
encoding other than base64 that will yield better results?

Feb 10 '07 #4
<je******@gmail.comwrote:
Don't try to encode arbitrary binary data as a string directly using
Encoding.Unicode. As Morten suggested, use Base64 instead.

Yeah, that's what I ended up doing but that's not much of a
compression since base64 makes the data a lot larger. Is there another
encoding other than base64 that will yield better results?
Not with the same degree of safety. If you could store the raw
compressed data instead of converting it back to a string, you'd be
okay - but to convert arbitrary binary data into text data which is
"safe" in many situations (i.e. won't be subject to unicode
normalization, can be expressed in many encodings etc) Base64 is a very
good choice.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 10 '07 #5
je******@gmail.com wrote:
On Feb 8, 1:55 am, Jon Skeet [C# MVP] <s...@pobox.comwrote:
> <jerem...@gmail.comwrote:
>>I'm writing some code that will convert a regular string to a byte[]
for compression and then beable to convert that compressed string back
into original form.
Don't try to encode arbitrary binary data as a string directly using
Encoding.Unicode. As Morten suggested, use Base64 instead.
Yeah, that's what I ended up doing but that's not much of a
compression since base64 makes the data a lot larger. Is there another
encoding other than base64 that will yield better results?
Nothing standard.

Using a home made Base128 together with using a single byte
encoding like ISO8859-1 will reduce the overhead slightly.

Arne
Feb 10 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
6
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how...
0
by: JEB30084 | last post by:
Hello, I'm trying to copy a complex structure to a string, encrypt the string, and then write the string to a file. I've tried using the marshal class, but can't get anything to work. When I...
5
by: Jamie Risk | last post by:
This is the code snippet that I've come up to convert a byte to string. Is there a best practiced method for such a conversion? - Jamie public static string ByteArrayToString(byte array) {...
1
by: mfunkmann | last post by:
Hi there, I have a really strange conversion-problem here: -- string _content = new string("some data"); ... System.Text.Encoding _Convert= new System.Text.ASCIIEncoding();
3
by: =?Utf-8?B?ai5hLiBoYXJyaW1hbg==?= | last post by:
Hello, I've eliminated the bulk of code, this should be sufficient: byte fromEncrypt; string sDecryptedString; //Read the data out of the crypto stream. csDecrypt.Read(fromEncrypt, 0,...
4
by: arunfr | last post by:
Hi , I have tried all the methods available to convert byte () to string and vice versa in VBSCRIPT. Function ByteArray2Text(varByteArray) Dim rs Const adLongVarChar = 201 Set rs =...
3
by: =?Utf-8?B?c3BkMzAwMQ==?= | last post by:
I have a C++/CLI app that uses a logging control in an ATL COM Server. C++ Interop seems to work just fine to pass data from the C++/CLI app to the ATL Control but I can't seem to figure out how...
3
by: shadabahmad | last post by:
Hi All! I had tried to search many forums to get this code. Yes, just for a single line code. But when everything else failed, I decided to write my own. And it is really working good. ...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.