C# 字符串string和内存流MemoryStream及比特数组byte[]之间相互转换
定义string变量为str,内存流变量为ms,比特数组为bt
1.字符串转比特数组
(2)byte[] bt=Convert.FromBase64String("字符串");
2.字符串转流
(2)MemoryStream ms=new MemoryStream(Convert.FromBase64String("字符串"));
3.流转比特数组
(2)MemoryStream ms=new MemoryStream();ms.Write(bt,0,ms.Length);
4.流转字符串
(2)string str=System.Text.Encoding.Default.GetString(ms.ToArray());
5.比特数组转字符串
(2)string str=Convert.ToBase64String(bt);
6.比特数组转流
(2)MemoryStream ms=new MemoryStream();ms.Read(bt,0,bt.Length);
总结:可以看出byte[]在字符串string和流MemoryStream之间转换起到过渡的作用,string和MemoryStream转换都要先转换成byte[]。
以下是将文件转为byte[]
/// <summary>
/// 将文件转换成byte[] 数组
/// </summary>
/// <param name="fileUrl">文件路径文件名称</param>
/// <returns>byte[]</returns>
protected byte[] GetFileData(string fileUrl)
{
FileStream fs = new FileStream(fileUrl, FileMode.Open, FileAccess.Read);
try
{
byte[] buffur = new byte[fs.Length];
fs.Read(buffur, 0, (int)fs.Length); return buffur;
}
catch (Exception ex)
{
//MessageBoxHelper.ShowPrompt(ex.Message);
return null;
}
finally
{
if (fs != null)
{ //关闭资源
fs.Close();
}
}
} /// <summary>
/// 将文件转换成byte[] 数组
/// </summary>
/// <param name="fileUrl">文件路径文件名称</param>
/// <returns>byte[]</returns> protected byte[] AuthGetFileData(string fileUrl)
{
using (FileStream fs = new FileStream(fileUrl, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
byte[] buffur = new byte[fs.Length];
using (BinaryWriter bw = new BinaryWriter(fs))
{
bw.Write(buffur);
bw.Close();
}
return buffur;
}
}

C# 字符串string和内存流MemoryStream及比特数组byte[]之间相互转换的更多相关文章
- 字符串string和内存流MemoryStream及比特数组byte[]互转
原文:字符串string和内存流MemoryStream及比特数组byte[]互转 字符串string和内存流MemoryStream及比特数组byte[]互转比较 定义string变量为str, ...
- c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换
字节数组byte[]与图片image之间的转化 字节数组转换成图片 public static Image byte2img(byte[] buffer) { MemoryStream ms = ne ...
- 文件读写(三)利用FileStream类操作字节数组byte[]、BinaryFormatter、内存流MemoryStream
一.Stream类概述 在.NET Framework中,文件和流是有区别的.文件是存储在磁盘上的数据集,它具有名称和相应的路径.当打开一个文件并对其进行读/写时,该文件就称为流(stream).但是 ...
- c#将图片Image转换为内存流MemoryStream
HttpWebResponse resp = (HttpWebResponse)wRequest.GetResponse(); Stream stream = resp.GetResponseStre ...
- java中String\十六进制String\byte[]之间相互转换函数
java二进制,字节数组,字符,十六进制,BCD编码转换2007-06-07 00:17/** *//** * 把16进制字符串转换成字节数组 * @param hex * @return */ pu ...
- 字符串string 、byte[]、MemoryStream、Base64String的相互转换
1.字符串转比特数组 (1)byte[] bt=System.Text.Encoding.Default.GetBytes("字符串"); (2)byte[] bt=Convert ...
- C#流总结(文件流、内存流、网络流、BufferedStream、StreamReader/StreamWriter、TextReader/TextWriter)
一.文件流 FileStream类主要用于读写磁盘文件.常用于向磁盘存储数据或读取配置文件. 读取文件: //文件流:读取 FileStream fileStream = File.Open(@&qu ...
- .net 流(Stream) - 文件流、内存流、网络流
转自:http://www.oseye.net/user/kevin/blog/85 一.文件流 FileStream FileStream流继承与Stream类,一个FileStream类的实例实际 ...
- [Java初探04]__字符串(String类)相关
前言 接下来将暂时将重心偏移向实际操作,不在将大量时间花费在详细的知识点整理上,将会简略知识总结笔记的记录,加强实际练习的时间,实例练习篇也不再同步进行,我会将部分我觉得重要的源码更新在每节知识点后面 ...
随机推荐
- Kotlin Hello World
{ https://github.com/zhmmmm/Kotlin }
- delphi 安卓开发常用
delphi 安卓开发有几个常用的pas: FMX.Helpers.Android, Androidapi.JNI.Net, Androidapi.JNI.GraphicsContentViewTex ...
- Go 动态类型声明
Go 动态类型声明 package main import "fmt" func main() { var x float64 = 20.0 y := 42 fmt.Println ...
- JZOJ5153:树形图求和
Description Input Output HINT 题解: 一种很直观的想法是通过矩阵生成树求树形图方法数ans以及不包含某一条边i的树形图方法数ans[i],则答案为Σ(ans-ans[i] ...
- NXOpenC#_Training_blockstyler(cn)【转载】
- 说说ReactiveCocoa 2
http://www.cocoachina.com/applenews/devnews/2014/0115/7702.html 转自无网不剩的博客 ReactiveCocoa是Github开源 ...
- row_number() over(partition by a order by b desc) rn 用法
转载于:http://www.blogjava.net/kxbin/articles/360195.html 可以看看http://jingyan.baidu.com/article/9989c746 ...
- CodeForces-1244C-The Football Season-思维
The football season has just ended in Berland. According to the rules of Berland football, each matc ...
- Python3 From Zero——{最初的意识:003~数字、日期、时间}
一.对数值进行取整:round(value,ndigits) >>> round(15.5,-1) #可以取负数 20.0 >>> round(15.5,0) #当 ...
- 拾遗:『rhel6系列的开、关机脚本』
一.系统脚本位置及含义 /etc/inittab./etc/init/* 存放开关机配置文件 /etc/init.d/* 服务脚本,是一个到/etc/rc.d/init.d/的软链接 /etc/rc. ...