分类
学习笔记

Base64字符串编码/解码

示例代码:

c#:

public static string EncodeBase64(string str)
        {
            return Convert.ToBase64String(Encoding.UTF8.GetBytes(str));
        }
public static string DecodeBase64(string code)
        {
            System.Text.Encoding encoding = new System.Text.UTF8Encoding();
            return encoding.GetString(Convert.FromBase64String(code));
        }

java:

import java.io.IOException;
public class hello {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
        String aa="我爱hello world";
        String str1=EncodeBase64(aa);
        String str2=DecodeBase64(str1);
        System.out.println("----aa:"+str1+str2);
}
///base64解码
public static String DecodeBase64(String code) throws IOException
     {
sun.misc.BASE64Decoder   decoder=new   sun.misc.BASE64Decoder();  
         byte[]  t=   decoder.decodeBuffer(new   String(code.getBytes("utf-8")));  
         return  new  String(t,"utf-8");  
     }
///base64编码
public static String EncodeBase64(String code)  throws IOException
{
byte[] bstr=code.getBytes("UTF-8");
         sun.misc.BASE64Encoder encoder=new sun.misc.BASE64Encoder();
         return encoder.encode(bstr);
}
}

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据