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! |