stream 、 string 、byte[] 间的相互转换 扩展方法
来源:互联网 发布:淘宝管控交易风险保障 编辑:程序博客网 时间:2024/06/10 16:19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
namespace Ims.Bll
{
/// <summary>
/// stream 、 string 、byte[] 间的转换扩展方法类
/// </summary>
public static class StreamExtend
{
#region Stream 扩展
/// <summary>
/// Stream Stream 转换为 byte 数组
/// </summary>
/// <returns></returns>
public static byte[] ToByteArray(this Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
/// <summary>
/// Stream 转换为 image 图片
/// </summary>
/// <returns></returns>
public static Image ToImage(this Stream stream)
{
Image img = new Bitmap(stream);
return img;
}
/// <summary>
/// Stream 转换为 string ,使用 Encoding.Default 编码
/// </summary>
/// <returns></returns>
public static string ToStr(this Stream stream)
{
return System.Text.Encoding.Default.GetString(stream.ToByteArray());
}
#endregion
#region byte[] 扩展
/// <summary>
/// byte[] 转换为 stream 流
/// </summary>
/// <returns></returns>
public static Stream ToStream(this byte[] arr)
{
Stream stream = new MemoryStream(arr);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
return stream;
}
/// <summary>
/// byte[] 转换为 Image
/// </summary>
/// <returns></returns>
public static Image ToImage(this byte[] arr)
{
return Image.FromStream(arr.ToStream());
}
/// <summary>
/// 转换为 string,使用 Encoding.Default 编码
/// </summary>
/// <returns></returns>
public static string ToStr(this byte[] arr)
{
return System.Text.Encoding.Default.GetString(arr);
}
#endregion
#region string 扩展
/// <summary>
/// string 转换为 byte[]
/// </summary>
/// <returns></returns>
public static byte[] ToByteArray(this string str)
{
return System.Text.Encoding.Default.GetBytes(str);
}
/// <summary>
/// string 转换为 Stream
/// </summary>
/// <returns></returns>
public static Stream ToStream(this string str)
{
Stream stream = new MemoryStream(str.ToByteArray());
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
return stream;
}
#endregion
}
}
-----------------------------------------------
//测试代码
int sourceLength = 0; //数据源长度
long byteArrLength = 0; //byte[] 的长度
long streamLength = 0; //Stream 的长度
// char 类型
char c = 'A';
byte[] byteArr_c = c.ToString().ToByteArray();
Stream sm_c = c.ToString().ToStream();
sourceLength = 1;
byteArrLength = byteArr_c.Length;
streamLength = sm_c.Length;
//string 类型
string str = "ABCDEF";
byte[] byteArr_str = str.ToByteArray();
Stream sm_str = str.ToStream();
sourceLength = str.Length;
byteArrLength = byteArr_str.Length;
streamLength = sm_str.Length;
//汉字
string cn = "好";
byte[] byteArr_cn = cn.ToByteArray();
Stream sm_cn = str.ToStream();
sourceLength = cn.Length;
byteArrLength = byteArr_cn.Length;
streamLength = sm_cn.Length;
char cn_l = (char)byteArr_cn[0];
char cn_r = (char)byteArr_cn[1];
技术支持:http://blog.csdn.net/xxj_jing
对于上面的代码 http://blog.csdn.net/xxj_jing/article/details/7514046 里进行了部分扩展,支持了 stream和byte 的搜索、查找方法!
- stream 、 string 、byte[] 间的相互转换 扩展方法
- String byte[] stream File之间的相互转换
- byte[] string相互转换
- String和byte[]之间的相互转换
- Java String与byte[]的相互转换
- c# Bitmap byte[] Stream 文件相互转换
- c# Bitmap byte[] Stream 文件相互转换
- c# Bitmap byte[] Stream 文件相互转换
- c# Bitmap byte[] Stream 文件相互转换
- C# Stream 和 byte[] 相互转换
- byte[]与string相互转换
- go string []byte相互转换
- Stream & Byte[]的互相转换
- C#下byte数组与string的相互转换
- Java String 和 byte[] 之间的相互转换
- C#中byte 和 string 的相互转换
- C#.NET中string与byte数组的相互转换
- c#中byte[]与string的相互转换
- IIS下架设网站账户权限配置
- 算法小题2→素数
- hibernate 类生成表 的实现源码
- 金山卫士开源软件之旅(八) netmon下netmon工程的解析---netmon.exe的启动过程分析
- 数据库之搜索效率及优化
- stream 、 string 、byte[] 间的相互转换 扩展方法
- 认识“数据可视化”和“信息可视化”
- 谷歌管理员工具提示robots文件”?User-agent: *”语法有误解决办法
- strace实现原理
- win7下android开发环境搭建(win7 64位)
- 今天你“导”了吗?
- dialog,activity 屏蔽Home键详解
- 移动性和IT技术缺口催生云统一通信
- WaitForSingleObject和CEvent的结合使用