C # stream byte char string stream, byte, character, string

Source: Internet
Author: User

1. Two different methods calculate the length of a string

String strtmp = "wk986 Wang Kedong ";

Int I = system. Text. encoding. Default. getbytes (strtmp). length;// Calculate the length of Chinese Characters
Int J = strtmp. length;// The length of a Chinese character is not counted

Console. writeline ("string {0}, count the length of Chinese characters: {1}, not count the length of Chinese characters: {2}", strtmp, I, j );

// Convert to an array to calculate the length of the array

Byte [] bytstr = system. Text. encoding. Default. getbytes (strtmp );
Int Len = bytstr. length;
Console. writeline ("String Length:" + Len. tostring ());
Console. Read ();

 

2. system. Text. stringbuilder ("")

It is different from the string "+". In C #, the string is of the "Reference" type. Each addition creates a new string. When the string is very large, high Performance consumption, so use stringbuilder.

System. Text. stringbuilder sb = new system. Text. stringbuilder ("");
SB. append ("China ");
SB. append ("people ");
SB. append ("Republic ");
Console. writeline (SB );

// Determine the number of Chinese Characters

Private int chkgbklen (string Str)
{
System. Text. asciiencoding n = new system. Text. asciiencoding ();
Byte [] B = n. getbytes (STR );
Int L = 0;
For (INT I = 0; I <= B. Length-1; I ++)
{
If (B [I] = 63) // determines whether it is a Chinese character or full-legged symbol.
{
L ++;
}
}
Return L;
}

C # stream, byte, character, string

First, you must understand what they are composed:

Stream:Binary

Byte:Unsigned integer

Character:Unicode characters

String:Multiple Unicode characters

In. net, how can they be converted?

Generally, the following rules are observed:

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

The conversion syntax is described as follows:

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!

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.