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