如何编码和解码Base128字符串在C#(3.5框架) [英] How to Encoding and Decoding Base128 strings in C#(3.5 framework)

查看:531
本文介绍了如何编码和解码Base128字符串在C#(3.5框架)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作与安全相关的项目,该项目目前我转换后的数据为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 可满足无头痛您的需求。看到这个<一href="http://stackoverflow.com/questions/976975/how-do-i-convert-from-8-bit-byte-to-7-bit-byte-base-256-to-base-128">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;
        }

这篇关于如何编码和解码Base128字符串在C#(3.5框架)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆