[转]
图片:
1 //编码例子
2 System.IO.FileStream fs = System.IO.File.OpenRead("c://1.jpg");
3 byte[] dt = new byte[fs.Length];
4 fs.Read(dt, 0, (int)fs.Length);
5 fs.Close();
6 string s = Convert.ToBase64String(dt);
7 fs = System.IO.File.OpenWrite("c://1.b64");
8 dt = Encoding.Default.GetBytes(s);
9 fs.Write(dt, 0, dt.Length);
10 fs.Flush();
11 fs.Close();
12 //解码例子
13 System.IO.FileStream fs = System.IO.File.OpenRead("c://1.b64");
14 byte[] dt = new byte[fs.Length];
15 fs.Read(dt, 0, (int)fs.Length);
16 string s = Encoding.Default.GetString(dt);
17 dt = Convert.FromBase64String(s);
18 fs = System.IO.File.OpenWrite("c://2.jpg");
19 fs.Write(dt, 0, dt.Length);
20 fs.Close();
文字:
编码:
byte[] bytes=Encoding.Default.GetBytes("要转换的字符串");
Convert.ToBase64String(bytes);
Convert.ToBase64String(bytes);
解码:
//"ztKwrsTj"是“我爱你”的base64编码
byte[] outputb = Convert.FromBase64String("ztKwrsTj");
string orgStr= Encoding.Default.GetString(outputb);
byte[] outputb = Convert.FromBase64String("ztKwrsTj");
string orgStr= Encoding.Default.GetString(outputb);