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://www.iqingcao.com转载请保留!
对于上面的代码 http://blog.csdn.net/xxj_jing/article/details/7514046里进行了部分扩展,支持了 stream和byte 的搜索、查找方法!
分享到:
相关推荐
将图片由bitmap类型转换为stream类型再转换为byte[]类型,然后再还原
本文以实例形式详细介绍了C#实现Stream与byte[]之间的转换的方法,分享给大家供大家参考之用。具体方法如下: 一、二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = 0; Image img = ...
Stream 和 byte[] 之间的转换
c++中stringstream的用法 istringstream, ostringstream, stringstream用法示例
1 split字符串 之前在用C#写代码的时候,用过split函数,可以把一个字符串根据某个分隔符分成若干个字符串数组。...stringstream ss(inputString); string tmp; while(getline(ss,tmp,'/')) { if(tmp.empty
自己总结的stringstream的应用,重点介绍了了其使用注意事项,希望对大家有帮助。
(2)byte[] bt=Convert.FromBase64String(“字符串”); 2.字符串转流 代码如下:(1)MemoryStream ms=new MemoryStream(System.Text.Encoding.Default.GetBytes(“字符串”)); (2)MemoryStream ms=new MemoryStream...
MemoryStream与liststring与stringstream转换
图片转二进制,二进制转图片,用XML保存二进制图片
本文实例讲述了C++利用stringstream进行数据类型转换的方法。分享给大家供大家参考。具体分析如下: 在C++标准库里面,使用stringstream 可以用于各种数据类型之间的转换 例如string 转 int 代码如下:std::string ...
streamstring在调用str()时,会返回临时的string对象。而因为是临时的对象,所以它在整个表达式结束后将会被析构。 如果需要进一步操作string对象,先把其值赋给一个string变量后再操作。 stringstream ss...
将字符串转换为流(streams2) 安装 npm install string-to-stream 用法 像这样使用string-to-stream : var str = require ( 'string-to-stream' ) str ( 'hi there' ) . pipe ( process . stdout ) // => 'hi ...
方法描述清除清除现有流fromArrayBuffer 从现有的“ ArrayBuffer”初始化“ Stream”对象fromUint8Array 从现有的“ Uint8Array”初始化“ Stream”对象fromString 从现有字符串初始化“ Stream”对象toString 将...
在本篇文章里小编给大家分享了关于通过stringstream实现常用的类型转换实例代码内容,需要的朋友们可以参考下。
本文将给大家详细介绍关于Stream和Buffer相互转换的相关资料,下面话不多说,来一起看看详细的介绍吧。 方法如下: Stream,中文叫做流,和我们平时充值信仰的那个 Steam 还是差了那么一点的。所谓流,是一种消费的...
之前在leetcode中进行string和int的转化时使用过istringstream,现在大致总结一下用法和测试用例。 介绍:C++引入了ostringstream、istringstream、stringstream这三个类,要使用他们创建对象就必须包含sstream.h...
前端开源库-stream-string流字符串,“将”流转换为字符串。默认情况下使用承诺,也允许回调。