欢迎访问!
您的支持是我们不断前行的动力!

C# 字节数组与Base64编码互转,字符串与Base64编码互转

如需将图片转为base64编码,请先将图片转为byte[] 字节数组,转换方法可参考 C# 图片和字节数组byte[]互转

    /// <summary>
    /// 将字节数组为Base64编码
    /// </summary>
    /// <param name="bytebase">要编码的byte[]</param>
    /// <returns></returns>
    public static string ByteToBase64(byte[] bytebase)
    {
        return Convert.ToBase64String(bytebase);
    }
    /// <summary>
    /// 将Base64字符串转为byte[]
    /// </summary>
    /// <param name="str">Base64字符串</param>
    /// <returns></returns>
    public static byte[] Base64ToByte(string str)
    {
        return Convert.FromBase64String(str);
    }
    /// <summary>
    /// 将字符串转为Base64编码
    /// </summary>
    /// <param name="str">要编码的string字符</param>
    /// <param name="encoding">字符编码方案</param>
    /// <returns></returns>
    public static string StringToBase64(string str, Encoding encoding)
    {
        byte[] buff = encoding.GetBytes(str);
        return Convert.ToBase64String(buff);
    }
    /// <summary>
    /// 将Base64编码解析成字符串
    /// </summary>
    /// <param name="strbase">要解码的string字符</param>
    /// <param name="encoding">字符编码方案</param>
    /// <returns></returns>
    public static string Base64ToString(string strbase, Encoding encoding)
    {
        byte[] buff = Convert.FromBase64String(strbase);
        return encoding.GetString(buff);
    }

 

赞(1) 打赏
转载请注明出处,未注明出处不得转载,蜘蛛侠 » C# 字节数组与Base64编码互转,字符串与Base64编码互转
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

蜘蛛侠,您身边的爬虫专家!

觉得文章有用就打赏一下文章作者吧

支付宝扫一扫打赏

微信扫一扫打赏