合作联系微信: w6668263      合作联系电话:177-9238-7426     

C#二进制与字符串之间的相互转换

/// <summary>
    /// 将字符串转成二进制
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string bianma(string s)
    {
        byte[] data = Encoding.Unicode.GetBytes(s);
        StringBuilder result = new StringBuilder(data.Length * 8);

        foreach (byte b in data)
        {
            result.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
        }
        return result.ToString();
    }


/// <summary>
    /// 将二进制转成字符串
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string jiema(string s)
    {
        System.Text.RegularExpressions.CaptureCollection cs =
            System.Text.RegularExpressions.Regex.Match(s, @"([01]{8})+").Groups[1].Captures;
        byte[] data = new byte[cs.Count];
        for (int i = 0; i < cs.Count; i++)
        {
            data[i] = Convert.ToByte(cs[i].Value, 2);
        }
        return Encoding.Unicode.GetString(data, 0, data.Length);
    }

 

posted on 2015-06-08 14:32  龙行龘龘9527  阅读(5420)  评论(0编辑  收藏  举报

导航