This article describes how to convert between byte[] and string.
Bit (b): The bit is only 0 1, 1 represents a pulse, and 0 represents no pulse. It is the most basic unit of computer physical memory storage.
Byte (B): 8 bits, 0-255 integer representation
Encoding: The character must be encoded before it can be processed by the computer. Early computers used 7 for ASCII encoding, and Chinese Simplified GB2312 and Big5 were designed to handle Chinese characters.
The conversion between a string and a byte array is, in fact, a conversion between the real world information and the digital world information, which is bound to involve some kind of encoding, and different encoding methods will result in different conversion results. System.Text.Encoding is often used in C # to manage commonly used encodings. The following direct code:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks; Namespace Bytetostring {class Program {static void Main (string[] args) {string str = "Sungozhen handsome!"
";
Use UTF encoding ...
byte[] UTF8 = strtobyte (str, ENCODING.UTF8);
It is estimated that C # was not designed at the time of Chinese Simplified, this is later in China?
byte[] gb2312 = Strtobyte (str,encoding.getencoding ("GB2312")); Console.WriteLine ("This is UTF8 (Sungozhen handsome), length is: {0}", UTF8.
Length);
foreach (var item in UTF8) {Console.Write (item);} Console.WriteLine ("N ' gb2312 (Sungozhen handsome), length is: {0}", gb2312.
Length);
foreach (var item in gb2312) {Console.Write (item)}/////////UTF8 encoded byte array to str string utf8str = Bytetostr (Utf8,encoding.utf8);
String gb2312str = Bytetostr (gb2312,encoding.getencoding ("GB2312"));
Console.WriteLine ("\n\nutf8: {0}", UTF8STR);
Console.WriteLine ("gb2312: {0}", GB2312STR);
Console.readkey (); //c# typically uses System.Text.Encoding encoding//string-array static byte[] Strtobyte (String str, Encoding Encoding) {return Encoding.
GetBytes (str); }//Array conversion string static string Bytetostr byTe[] bt,encoding Encoding) {return Encoding.
GetString (BT); }
}
}
The above is a small series to introduce the C # byte[] and string conversion between the method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!