将字符转换为字节:
byte[] byteTime = Encoding.ASCII.GetBytes(DateTime.Now.ToString());
将字节转换为字符:
string sTime = Encoding.ASCII.GetString(bytes,0,bytesRead);
string strText = "This is an original string";
System.Text.UTF8Encoding encText = new System.Text.UTF8Encoding();
byte[] btText;
btText = encText.GetBytes(strText);
MessageBox.Show("The total number of encoded bytes is: " + btText.Length.ToString());
vb.net
Dim strText As String = "This is an original string"
Dim encText As New System.Text.UTF8Encoding()
Dim btText() As Byte
btText = encText.GetBytes(strText)
MessageBox.Show("The total number of encoded bytes is: " & btText.Length.ToString())