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#?
-
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. – ericosg Jun 05 '13 at 04: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. – Praveen Kumar Jun 05 '13 at 04:48
-
1Try yEnc http://www.codeproject.com/Articles/6548/A-C-component-to-encode-and-decode-yEnc-data – ericosg Jun 05 '13 at 05:00
-
2And if you use base256 it will be even smaller! – x2. Jun 05 '13 at 05:01
-
http://stackoverflow.com/a/6008076/159769 – x2. Jun 05 '13 at 05:01
-
@x2. ok...Hey how to implement base256 encoding technique? you have any idea? – Praveen Kumar Jun 05 '13 at 05:04
-
Why you want to save images in text format(string)? You can store it as byte array. But if you need strings, its better base64 than base128. – x2. Jun 05 '13 at 05:11
-
@x2. I will encode that string data in QRCode. If the length of the string is more, then the size of the QRCode will increase.To reduce the QRCode size ,if i use base128 encoding technique then length of the string will be reduced... – Praveen Kumar Jun 05 '13 at 05:20
-
2The 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? – Jarek Jun 05 '13 at 06:35
1 Answers
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;
}

- 1
- 1

- 1,913
- 16
- 23
-
Jhonson ok..but I am working on .Net 3.5 framework,So "BigInteger" is not avialable in this framework right..? – Praveen Kumar Jun 06 '13 at 05:26
-
@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. – Matt Johnson Jun 06 '13 at 21:11