Base64文字列的变换(VB.NET和C#)

前端之家收集整理的这篇文章主要介绍了Base64文字列的变换(VB.NET和C#)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

和大家分享一下Base64文字列转换方法。本文来自http://bingning.net/VB/SOURCE/string/base64.html

字节型数组向Base64文字列的变换

[VB.NET]

'变换成Base64文字列的文件
Dim inFileName As String = "C:/test.zip"
Dim inFile As System.IO.FileStream
Dim bs() As Byte

'文件以byte型数组的形式全部读入
inFile = New System.IO.FileStream(inFileName,_
System.IO.FileMode.Open,System.IO.FileAccess.Read)
ReDim bs(inFile.Length - 1)
Dim readBytes As Long = inFile.Read(bs,inFile.Length)
inFile.Close()

'变换成Base64文字
Dim base64String As String
base64String = System.Convert.ToBase64String(bs)

'表示结果
Console.WriteLine(base64String)

C#]

//变换成Base64文字列的文件
string inFileName = @"C:/test.zip";
System.IO.FileStream inFile;
byte [] bs;

//文件以byte型数组的形式全部读入
inFile = new System.IO.FileStream(inFileName,
System.IO.FileMode.Open,System.IO.FileAccess.Read);
bs = new byte [inFile.Length];
int readBytes = inFile.Read(bs,(int) inFile.Length);
inFile.Close();

//变换成Base64文字
string base64String;
base64String = System.Convert.ToBase64String(bs);

//表示结果
Console.WriteLine(base64String);

从Base64文字列向字节型数组的变换
[VB.NET]

'返回Base64字节型数组的文字
Dim s As String = TextBox1.Text

'返回字节型数组
Dim bs() As Byte = System.Convert.FromBase64String(s)

'保存到文件
'保存的文件
Dim outFileName As String = "C:/test.dat"
'写入到文件
Dim outFile As New System.IO.FileStream(outFileName,_
System.IO.FileMode.Create,System.IO.FileAccess.Write)
outFile.Write(bs,bs.Length)
outFile.Close()

 [C#]

 //返回Base64字节型数组的文字
 string s = TextBox1.Text;

 //返回字节型数组
 byte [] bs = System.Convert.FromBase64String(s);

 //保存到文件
 //保存的文件
 string outFileName = @"C:/test.dat";
 //写入到文件
 System.IO.FileStream outFile = new System.IO.FileStream(outFileName,System.IO.FileMode.Create,System.IO.FileAccess.Write);
 outFile.Write(bs,bs.Length);
 outFile.Close();

猜你在找的VB相关文章