首页 新闻 会员 周边

求助啊 二进制与中英文字符的相互转换

0
悬赏园豆:20 [已关闭问题]

我想将中英文字符串转换成二进制,在将二进制转换成中英文字符串,字符串转换成二进制 已经转换好了,可是二进制到字符串不会啊,哪位仁兄帮帮我啊,谢谢了!我二进制转换成字符串的思路是这样的:1,二进制字符转换成Byte数据,2,Byte数组转换成字符串。
//字符串转换成二进制  

    public static string str_zhuanh(string yincx)
    {

        string s1="";
        byte[] s3 = System.Text.Encoding.ASCII.GetBytes(yincx);

        for (int i = 0; i < s3.Length; i++)
        {
            s1+=System.Convert.ToString(s3[i],2);
        }
  return s1;
 }

 

//二进制转换成byte(这个函数有问题啊谁帮我看看,谢谢啊!)
protected static byte zhuan(string s)
    {
        int a=0;
       
       for (byte i = 0; i < 8; i++)
        {
            if (s[i].ToString().Equals("1"))
            {
                a += (int)Math.Pow(2, 7 - i);
            }
        
        }
        //a = System.Convert.ToInt32(s);
        byte d = System.Convert.ToByte(a);
        return d;
    }

//byte转换成字符串
protected static string str_zhuann(string yince){
  string s3="",startstr="",s2="";
        int MAX = yince.Length/8,j=0;
        byte[] s1=new byte[MAX*2];
        for (int i = 0; i < MAX; i ++)
        {
            for (j = i*8; j < i*8 + 8; j++)
            {
                startstr += yince[j].ToString();
         &n

水水的主页 水水 | 初学一级 | 园豆:115
提问于:2009-05-16 16:52
< >
分享
其他回答(1)
0

你可以通过强制转化 或 Binary转化

龙的传人88 | 园豆:220 (菜鸟二级) | 2009-05-16 19:19
0

你的代码有多处问题,

1. 采用 System.Convert.ToString(b, 2); 转换int 到二进制时,如果最高位是0,被省略了,导致二进制不是以8位为单位,这直接影响到你从二进制转换行回来时的解码。我在你的代码后面加上了补齐最高为0的代码。

2. 如果你要转换中英文字符,不能使用ASCII  编码应采用Unicode 编码,我将你的代码改成了UTF8

3. 二进制字符串转换为byte时用移位比较好。

4. String += 的操作如果字符串长度较大会影响效率,我改成了StringBuilder

其他还有一些写的不够简洁的代码我也给改了,你可以运行我写的代码,转换和反转换都没有问题了。

 

        public static string str_zhuanh(string yincx)
        {

            StringBuilder str = new StringBuilder();

            byte[] buf = System.Text.Encoding.UTF8.GetBytes(yincx);

            foreach (byte b in buf)
            {
                string binString = System.Convert.ToString(b, 2);

                while (binString.Length < 8)
                {
                    binString = "0" + binString;
                }

                str.Append(binString);

            }

            return str.ToString();
        }

        protected static byte zhuan(string s)
        {
            byte d = 0;

            for (byte i = 0; i < 8; i++)
            {
                d <<= 1;

                if (s[i] == '1')
                {
                    d += 1;
                }

            }

            return d;
        }

        protected static string str_zhuann(string yince)
        {
            byte[] buf = new byte[yince.Length / 8];

            for (int i = 0; i < buf.Length; i++)
            {
                buf[i] = zhuan(yince.Substring(i * 8));
            }

            return System.Text.Encoding.UTF8.GetString(buf);
        }

 

        static void Main(string[] args)
        {
            string binString = str_zhuanh("Chinese English convert to binary 中英文转换为二进制");
            Console.WriteLine(binString);

            Console.WriteLine(str_zhuann(binString));

        }

eaglet | 园豆:17139 (专家六级) | 2009-05-18 08:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册