如何编码和解码Base128字符串在C#(3.5框架)字符串、框架

由网友(余孽)分享简介:我的工作与安全相关的项目,该项目目前我转换后的数据为base64 format.But转换后的base64数据量较多,为此,我想转换一个数据base128 format.How来恩$ C $在base128字符串格式c和德code数据在C#?I am working on security related proje...

我的工作与安全相关的项目,该项目目前我转换后的数据为base64 format.But转换后的base64数据量较多,为此,我想转换一个数据base128 format.How来恩$ C $在base128字符串格式c和德code数据在C#?

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#?

推荐答案

我会强烈反对Base128编码,如果你能避免它。有7位字母必须包含不可打印的ASCII控制字符(只有94个可打印字符又名低于$ C $口岸系统为0x20)。许多系统会摔倒,如果你试图给他们这种类型的数据。这真的似乎并不值得少量的空间节省,你会获得额外位。像 ASCII85 或 Base91 可满足无头痛您的需求。看到这个SO发布一个类似的问题。

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.

不过,如果你是持久的,那么你应该能够修改映射字符串中的下列以获得你所需要的。 (注意:您必须使用正确的不可打印code为您要添加到这个映射 x09表示字符):

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;
        }
阅读全文

相关推荐

最新文章