CSDN博客

img IMAGSE

C# Base64转换

发表于2012/3/1 16:38:36  7354人阅读

/// <summary>
    /// 用于文本和Base64编码文本的互相转换 和 Byte[]和Base64编码文本的互相转换
    /// </summary>
    class Base64Change
    {
        /// <summary>
        /// 将普通文本转换成Base64编码的文本
        /// </summary>
        /// <param name="value">普通文本</param>
        /// <returns></returns>
        public string StringToBase64String(String value)
        {
            byte[] binBuffer =(new UnicodeEncoding()).GetBytes(value);
            int base64ArraySize = (int)Math.Ceiling(binBuffer.Length / 3d) * 4;
            char[] charBuffer = new char[base64ArraySize];
            Convert.ToBase64CharArray(binBuffer, 0, binBuffer.Length, charBuffer, 0);
            string s = new string(charBuffer);
            return s;
        }

        /// <summary>
        /// 将Base64编码的文本转换成普通文本
        /// </summary>
        /// <param name="base64">Base64编码的文本</param>
        /// <returns></returns>
        public string Base64StringToString(string base64)
        {
            char[] charBuffer = base64.ToCharArray();
            byte[] bytes = Convert.FromBase64CharArray(charBuffer, 0, charBuffer.Length);
            return (new UnicodeEncoding()).GetString(bytes);
        }
       
        /// <summary>
        /// 将Byte[]转换成Base64编码文本
        /// </summary>
        /// <param name="binBuffer">Byte[]</param>
        /// <returns></returns>
        public string toBase64(byte[] binBuffer)
        {
            int base64ArraySize = (int)Math.Ceiling(binBuffer.Length / 3d) * 4;
            char[] charBuffer = new char[base64ArraySize];
            Convert.ToBase64CharArray(binBuffer, 0, binBuffer.Length, charBuffer, 0);
            string s = new string(charBuffer);
            return s;
        }

        /// <summary>
        /// 将Base64编码文本转换成Byte[]
        /// </summary>
        /// <param name="base64">Base64编码文本</param>
        /// <returns></returns>
        public Byte[] Base64ToBytes(string base64)
        {
            char[] charBuffer = base64.ToCharArray();
            byte[] bytes = Convert.FromBase64CharArray(charBuffer, 0, charBuffer.Length);
            return bytes;
        }
    }

应用:
文本转换
            Base64Change bc = new Base64Change();
            string s = "枯藤昏鸦";

            string bs = bc.StringToBase64String(s);//bs为转换成的base64编码文本

            s = bc.Base64StringToString(bs);//s为还原后的文本"枯藤昏鸦"

Byte[]转换

            Base64Change bc=new Base64Change();

            FileStream fs = new FileStream("d:\\a.bmp", FileMode.Open, FileAccess.ReadWrite);
            byte[] bt=new byte[fs.Length];
            fs.Read(bt, 0, (int)fs.Length);
            fs.Close();
            string s = bc.toBase64(bt);//s为转换成的base64编码文本

            byte[] b=bc.Base64ToBytes(s);//b为还原后的Byte[]
            FileStream f = new FileStream("d:\\b.bmp", FileMode.Create, FileAccess.Write);
            f.Write(b, 0, b.Length);
            f.Close();

 

转自:http://hi.baidu.com/85411235/blog/item/a4ced7a2b76d01874610643e.html

0 0

相关博文

我的热门文章

img
取 消
img