Usually Type Converter In Socket Transfor


As a chsarp developer ,you must have learned the socket ,and you know that the data transfor by network should be rely on byte or xml.

There I will give the usually type converter in socket transfor

1:string byte[] (use to fransfor message in socket)

(1):string convert to byte[]

string str="wulong";

byte[] byteArray = System.Text.Encoding.Default.GetBytes(str);

//you also could select your ownencode accoding to the requirement of project

(2)byte[] convert to string

byte[] byteArray;//you can get it by the socket,then convert to string

string str = System.Text.Encoding.Default.GetString(byteArray);

2:File byte[] (use to file transfor in socket)

(1):File to byte[]

string fileName="";

FileStreamfileStream=newFileStream(fileName,FileMode.Open,FileAccess.Read);

BinaryReaderbinaryReader=newBinaryReader(fileStream);

binaryReader.BaseStream.Seek(0,SeekOrigin.Begin);

byte[] bytes=binaryReader.ReadBytes((int)binaryReader.BaseStream.Length);


(2):byte[] to filestream

byte[] bytes;

FileStreamfileStream=newFileStream(fileName,FileMode.Open,FileAccess.Read);

fileStream.Write(bytes,0,bytes.Length);

then you could deal with the fileStream

3:Image byte[] (use transfor image in socket)

(1):Imagetobyte[]

Image image = Image.FromFile("filepath");
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.GetBuffer();

(2):byte[] to Image

byte[] byteImage;

MemoryStream ms = new MemoryStream(byteImage);
Image imageGetByBytes = Image.FromStream(ms);



优质内容筛选与推荐>>
1、UIViewSubviews多个views之间的关系
2、C#将PDF转成 EPUB
3、hunnu 11545小明的烦恼——找路径 (最大流)
4、19-11-08-+
5、深入理解系列之 float


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号