Usually type converter in socket transfor

Source: Internet
Author: User

As a chsarp developer, you must have learned the socket, and you know that the data transfor by network shoshould 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 cocould select your own encode 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 = "";

Filestream = new filestream (filename, filemode. Open, fileaccess. Read );

Binaryreader = new binaryreader (filestream );

Binaryreader. basestream. Seek (0, seekorigin. Begin );

Byte [] bytes = binaryreader. readbytes (INT) binaryreader. basestream. Length );


(2): byte [] to filestream

Byte [] bytes;

Filestream = new filestream (filename, filemode. Open, fileaccess. Read );

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

Then you coshould deal with the filestream

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

(1): image to byte []

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

 



 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.