编码文件为Base64编码形式和解码Base64编码文件
2007-11-13 17:09
295 查看
![](http://images.cnblogs.com/cnblogs_com/pochonlee/test.jpg)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
namespace ToBase64
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
byte[] binaryData;
byte[] frombaseData;
string base64String,frombase64;
private void button1_Click(object sender, EventArgs e)
{
try //读取当前目录下的base.txt文件
{
FileStream inFile = new System.IO.FileStream("base.txt",FileMode.Open,FileAccess.Read);
binaryData=new Byte[inFile.Length];
inFile.Read(binaryData, 0,(int)inFile.Length);
inFile.Close();
}
catch (Exception err)
{
label1.Text = err.Message.ToString();
}
try//转换
{
base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
}
catch(Exception err)
{
label1.Text=err.Message.ToString();
}
try//将转换后的数据写入到当前目录result.txt中
{
StreamWriter sw = new StreamWriter("result.txt", false, System.Text.Encoding.ASCII);
sw.WriteLine(base64String);
sw.Close();
}
catch (Exception err)
{
label1.Text = err.Message.ToString();
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{//读取上次转换后的文件
char[] base64CharArray;
StreamReader inFile = new StreamReader("result.txt", System.Text.Encoding.ASCII);
base64CharArray = new char[inFile.BaseStream.Length];
inFile.Read(base64CharArray, 0, (int)inFile.BaseStream.Length);
frombase64 = new string(base64CharArray);
}
catch(Exception err)
{
label1.Text = err.Message.ToString();
}
try
{//转换
frombaseData = System.Convert.FromBase64String(frombase64);
}
catch(Exception err)
{
label1.Text = err.Message.ToString();
}
try
{//写入新的文本文件,和原来的内容相符
FileStream fs = new FileStream("frombase64.txt", System.IO.FileMode.Create, System.IO.FileAccess.Write);
fs.Write(frombaseData, 0, binaryData.Length);
fs.Close();
}
catch (Exception err)
{
label1.Text = err.Message.ToString();
}
}
}
}
相关文章推荐
- (java)利用BASE64编码和解码图片文件
- java使用Base64编码和解码的图像文件
- 验证码以base64文件编码形式传输并显示前端
- mht文件解码,base64编码解码
- [置顶] Python语言处理整个文件的base64编码解码源码共享
- (java)利用BASE64编码和解码图片文件
- Python 和 VBScript 对文件编码-解码的方式 (Hex, Base64) 的疑问
- java利用Base64编码和解码图片文件
- java利用Base64编码和解码图片文件
- java利用Base64编码和解码图片文件
- 实用命令:利用openssl进行BASE64编码解码、md5/sha1摘要、AES/DES3加密解密
- Python中进行Base64编码和解码
- oracle中base64编码解码中文
- spring mvc源码-》MultipartReques类-》主要是对文件上传进行的处理,在上传文件时,编码格式为enctype="multipart/form-data"格式,以二进制形式提交数据,提交方式为post方式。
- Android中Base64编码与解码的实现
- openssl学习篇之base64编码、解码;md5 摘要;sha1摘要;3des加密,解密;rsa算法
- AS3的BASE64编码与解码类
- Base64编码与解码的java实现
- BASE64 编码解码
- base64 编码解码