Stream, byte, character, string in C #

Source: Internet
Author: User

First, you must understand what they are composed:

Stream: Binary

Byte: unsigned integer

Character: unicode encoded character

String: Multiple Unicode characters

 

In. net, how can they be converted?

Generally, the following rules are observed:

Stream-> byte array-> character array-> string

 

The following describes the conversion syntax.

Stream-> byte array

Memorystream MS = new memorystream ();

Byte [] buffer = new byte [Ms. Length];

Ms. Read (buffer, 0, (INT) ms. Length );

 

Byte array-> stream

Byte [] buffer = new byte [10];

Memorystream MS = new memorystream (buffer );

 

Byte array-> character array

1.

Byte [] buffer = new byte [10];

Char [] CH = new asciiencoding (). getchars (buffer );

// Or: Char [] CH = encoding. utf8.getchars (buffer)

2.

Byte [] buffer = new byte [10];

Char [] CH = new char [10];

For (INT I = 0; I <buffer. length; I ++)

{

Ch [I] = convert. tochar (buffer [I]);

}

 

Character array-> byte array

1.

Char [] CH = new char [10];

Byte [] buffer = new asciiencoding (). getbytes (CH );

// Or: byte [] buffer = encoding. utf8.getbytes (CH)

2.

Char [] CH = new char [10];

Byte [] buffer = new byte [10];

For (INT I = 0; I <Ch. length; I ++)

{

Buffer [I] = convert. tobyte (CH [I]);

}

 

Character array-> string

Char [] CH = new char [10];

String STR = new string (CH );

 

String> character array

String STR = "ABCDE ";

Char [] CH = Str. tochararray ();

 

Byte array-> string

Byte [] buffer = new byte [10];

String STR = system. Text. encoding. utf8.getstring (buffer );

// Or: String STR = new asciiencoding (). getstring (buffer );

 

String> byte array

String STR = "ABCDE ";

Byte [] buffer = system. Text. encoding. utf8.getbytes (STR );

// Or: byte [] buffer = new asciiencoding (). getbytes (STR );

 

Note: The convert class and the class in the system. Text namespace are used. encoding is a static class, asciiencoding is an entity class, and the methods are the same!

 

ReferenceArticle:

In C #, uint -- byte [] -- char [] -- string mutual conversion Summary

Http://www.cnblogs.com/huomm/archive/2008/08/29/1279417.html

How to convert a string to a stream?

Http://topic.csdn.net/t/20041224/17/3674276.html

What is the difference between char [] and byte?

Http://topic.csdn.net/t/20051102/14/4366558.html

Unsigned char in C ++! = Byte

Http://blog.csdn.net/lyskyly/archive/2008/10/02/3009582.aspx

Conversion between data streams and strings!

Http://blog.joycode.com/aspdian/archive/2004/02/29/14166.aspx

Convert file flow to string (convert binary to string)

Http://www.cnblogs.com/riverspirit/articles/1276268.html

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.