而且要能转回,应该用什么办法,谢谢
解决方案 »
- 哪位高人帮我检查下这个C#代码
- 递归算法无法中途跳出
- access 查询结果与返回DataSet结果不同
- C# 如何读取8位位图的RGB值
- 在窗体中拖 一个Datagridview控件,添加了三个列,运行后DatagridView中不是自动有一列吗??
- 用dotmsn开发过MSN机器人,问个有关dotmsn的问题
- 对输入的变量类型进行判断在输出的问题!
- C#.net中DataGrid里面e.Item.ItemType如何用?
- 只知道一个.h文件,怎么在C#中使用
- 请教public调用问题
- 很心疼的发50分问个问题(大家千万相信我的人品,我一定会给分的,偶的所有帖子偶都给分,历史证明的)一个文件操作的小问题,汗,大家帮帮忙吧,也许很简单
- 关于子窗体的问题
及
Convert.FromBase64String 方法
来对二进制数据和字符串相互转换:下面的代码示例演示如何使用 ToBase64CharArray 方法对二进制流进行 UUencode (base 64) 编码,然后将编码保存到文件中。public void EncodeWithCharArray() {
System.IO.FileStream inFile;
byte[] binaryData; try {
inFile = new System.IO.FileStream(inputFileName,
System.IO.FileMode.Open,
System.IO.FileAccess.Read);
binaryData = new Byte[inFile.Length];
long bytesRead = inFile.Read(binaryData, 0,
(int) inFile.Length);
inFile.Close();
}
catch (System.Exception exp) {
// Error creating stream or reading from it.
System.Console.WriteLine("{0}", exp.Message);
return;
} // Convert the binary input into Base64 UUEncoded output.
// Each 3 byte sequence in the source data becomes a 4 byte
// sequence in the character array.
long arrayLength = (long) ((4.0d/3.0d) * binaryData.Length);
// If array length is not divisible by 4, go up to the next
// multiple of 4.
if (arrayLength % 4 != 0) {
arrayLength += 4 - arrayLength % 4;
}
char[] base64CharArray = new char[arrayLength];
try {
System.Convert.ToBase64CharArray(binaryData,
0,
binaryData.Length,
base64CharArray,
0);
}
catch (System.ArgumentNullException) {
System.Console.WriteLine("Binary data array is null.");
return;
}
catch (System.ArgumentOutOfRangeException) {
System.Console.WriteLine("Char Array is not large enough.");
return;
} // Write the UUEncoded version to the output file.
System.IO.StreamWriter outFile;
try {
outFile = new System.IO.StreamWriter(outputFileName,
false,
System.Text.Encoding.ASCII);
outFile.Write(base64CharArray);
outFile.Close();
}
catch (System.Exception exp) {
// Error creating stream or writing to it.
System.Console.WriteLine("{0}", exp.Message);
}
}
下面的代码示例演示如何使用 FromBase64String 方法对以 UUencode (base 64) 编码的数据进行解码并将它保存为二进制输出。public void DecodeWithString() {
System.IO.StreamReader inFile;
string base64String; try {
char[] base64CharArray;
inFile = new System.IO.StreamReader(inputFileName,
System.Text.Encoding.ASCII);
base64CharArray = new char[inFile.BaseStream.Length];
inFile.Read(base64CharArray, 0, (int)inFile.BaseStream.Length);
base64String = new string(base64CharArray);
}
catch (System.Exception exp) {
// Error creating stream or reading from it.
System.Console.WriteLine("{0}", exp.Message);
return;
} // Convert the Base64 UUEncoded input into binary output.
byte[] binaryData;
try {
binaryData =
System.Convert.FromBase64String(base64String);
}
catch (System.ArgumentNullException) {
System.Console.WriteLine("Base 64 string is null.");
return;
}
catch (System.FormatException) {
System.Console.WriteLine("Base 64 string length is not " +
"4 or is not an even multiple of 4." );
return;
} // Write out the decoded data.
System.IO.FileStream outFile;
try {
outFile = new System.IO.FileStream(outputFileName,
System.IO.FileMode.Create,
System.IO.FileAccess.Write);
outFile.Write(binaryData, 0, binaryData.Length);
outFile.Close();
}
catch (System.Exception exp) {
// Error creating stream or writing to it.
System.Console.WriteLine("{0}", exp.Message);
}
}
----------
支持