1

I am working on security related project,in that project currently i converted data to base64 format.But size of converted base64 data is more,for this purpose i want to convert a data to base128 format.How to Encode and Decode data in base128 string format in c#?

9
  • how is it that the size is more? is the data an integer or ascii text? i think you are looking for something else rather than base128. Commented Jun 5, 2013 at 4:40
  • @ericosg i have a image, that i converted to base64 format, the size of the base64 data is 2.33KB. If we use base128 encoding technique instead of base64 encoding technique then size will be reduced. Commented Jun 5, 2013 at 4:48
  • 1
    Try yEnc codeproject.com/Articles/6548/… Commented Jun 5, 2013 at 5:00
  • 2
    And if you use base256 it will be even smaller! Commented Jun 5, 2013 at 5:01
  • 2
    The whole idea of Base64 is to encode binary data into printable string. Like linked answer in x2's comment - how would you like to output data as string when some characters will not be printable or unambiguous, when given encoding? Commented Jun 5, 2013 at 6:35

1 Answer 1

7

I would strongly recommend against Base128 encoding if you can avoid it. A 7 bit alphabet would have to contain unprintable ASCII control characters (there are only 94 printable characters aka below codepoint 0x20). Many systems will fall over if you attempt to give them this type of data. It really does not seem to be worth the small amount of space savings you would get for the extra bit. Something like ASCII85 or Base91 may satisfy your needs without the headaches. See this SO post on a similar question.

However if you are persistent then you should be able to modify the mapping string in the following to get what you need. (NOTE: you will have to use the correct unprintable code for the characters you want to add to mapping like this "\x09"):

public static string GetStringFromByteArray(byte[] data)
        {
            string mapping = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvqwxyz!#$%&()*+-;<=>?@^_`{|}~',./:[]\\\"";

            BigInteger base10 = new BigInteger(data);
            string baseX;
          int base=mapping.Length;
            var result = new Stack<char>();

            do
            {
                result.Push(mapping[(int)(base10 % base)]);
                base10 /= base;

            } while (base10 != 0);

            baseX = new string(result.ToArray());

            return baseX;
        }
Sign up to request clarification or add additional context in comments.

2 Comments

Jhonson ok..but I am working on .Net 3.5 framework,So "BigInteger" is not avialable in this framework right..?
@PraveenKumar Your question didn't say you were limited to 3.5. In any case, the BigInteger class just makes the algorithm easier to understand. You can use Byte manipulation of the data to only pull off every 7 bits, use that as an index into the mapping string. This will achieve the same affect as using BigInteger in the above code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.