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);