Re: Compress ASCII text as Hex?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: James Curran (JamesCurran_at_mvps.org)
Date: 11/11/04


Date: Thu, 11 Nov 2004 14:06:36 -0500


    There's a method, but it's a bit snarky....

There an encoding format code BASE64 (also known as UUEncoding in some
quarters). It take fully binary data (0-255) and converts it a set of 64
printable characters (digits, uppercase, lowercase plus two symbols + and
/). Since email messages are required to be pure printable text (due to
some ancient hardware, which are almost certainly no longer on the 'net),
all attachments are BASE64 encoded. It converts 3 binary bytes into 4
characters, so encoded blocks increase 33% in size.

So, what does this effect you? Well, as long as your "encoded" string meets
the criteria of Base64 encoding, you can "decode" it into a smaller block of
binary data. 4 characters will become 3 bytes, or in your case, 20
characters can become 15 bytes.

string origString = "123456,abcdef,ghijkl"; // 20 character CSV text

string prepareText = origString.Replace(',', '+'); // Replace commas with
plus signs
byte[] compressedText = Convert.FromBase64String(prepareText);
Console.WriteLine("Length of Conpressed text = {0}", compressedText.Length);
// Save compressedText to your store.
// :
// Later read it back
string alteredText = Convert.ToBase64String(compressedText);
string finalString = alteredText.Replace('+', ',');

Console.WriteLine("Text: {0}, this {1} the same as the original",
    finalString, finalString == origString ? "IS" : "IS NOT");

Running the above, I get:
Length of Conpressed text = 15
Text: 123456,abcdef,ghijkl, this IS the same as the original

-- 
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com       Work: www.njtheater.com
Blog: www.honestillusion.com  Day Job: www.partsearch.com
"Ben Bloom" <bbloom@macg.s.p.a.m.regor.com> wrote in message
news:ewsnLf2xEHA.2804@TK2MSFTNGP15.phx.gbl...
> Hi -
>
> I was speaking with someone who mentioned that it's possible to encode
> an ascii string as hex(?) in order to fit more data into the same # of
> chars.  Can anyone enlighten me?
>
> The scenario is - I've got a CSV with a field that has a 16 character
> limit.  I need to fit potentially 24 ASCII characters into it.
>
> Thanks.
> -Ben
> -- 
> to reply, remove .s.p.a.m. from email


Relevant Pages

  • Re: Copying string to byte array
    ... Notice how I was criticized for defining binary data ... Dim varInput As Variant ... Your comment says that varInput "has a string that has..." ... characters without telling you it did that. ...
    (microsoft.public.vb.general.discussion)
  • Re: data type lengths
    ... If you use SP_HELP to find the length of a column, it retrieves the length of VARCHAR columns in characters, but the length of NVARCHAR columns in bytes. ... The wierd thing I think the eBook versions that come with the books are out ... String or binary data would be truncated. ...
    (microsoft.public.sqlserver.programming)
  • Re: data type lengths
    ... I am new in SQL. ... varcharwill hold a maximum of x characters (each occupying one 8-bit ... String or binary data would be truncated. ...
    (microsoft.public.sqlserver.programming)
  • Re: Base64 question
    ... I convert the bytes to Base64. ... However, the result includes characters that are invalid for a URL, ... I have to cycle the output string through ... I think it's pretty common to adapt base64 to only include URL-safe ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Base64 question
    ... I convert the bytes to Base64. ... However, the result includes characters that are invalid for a URL, ... I have to cycle the output string through ... Base64 converter to use a character set that could avoid the trip through ...
    (microsoft.public.dotnet.languages.csharp)