string和byte[]的轉換(C#) - 我的記憶
文章推薦指數: 80 %
string和byte[]的轉換(C#). string類型轉成byte[]:. byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );. skiptomain| skiptosidebar 2012/03/20 string和byte[]的轉換(C#) string類型轉成byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str ); 反過來,byte[]轉成string: string str = System.Text.Encoding.Default.GetString ( byteArray ); 其它編碼方式的,如System.Text.UTF8Encoding,System.Text.UnicodeEncodingclass等;例如: string類型轉成ASCIIbyte[]:("01"轉成byte[]=newbyte[]{0x30,0x31}) byte[] byteArray = System.Text.Encoding.ASCII.GetBytes ( str ); ASCIIbyte[]轉成string:(byte[]=newbyte[]{0x30,0x31}轉成"01") string str = System.Text.Encoding.ASCII.GetString ( byteArray ); 有時候還有這樣一些需求: byte[]轉成原16進制格式的string,例如0xae00cf,轉換成"ae00cf";newbyte[]{0x30,0x31}轉成"3031": public static string ToHexString ( byte[] bytes ) // 0xae00cf => "AE00CF " { string hexString = string.Empty; if ( bytes != null ) { StringBuilder strB = new StringBuilder (); for ( int i = 0; i
延伸文章資訊
- 1Byte Objects vs String in Python - GeeksforGeeks
- 2How to Convert a Byte value to String value in Java with Examples
- 3Convert a byte array to a string in C# | Techie Delight
To decode all bytes in the byte array into a string, use the Encoding.GetString() method. Several...
- 4string和byte[]的轉換(C#) - 我的記憶
string和byte[]的轉換(C#). string類型轉成byte[]:. byte[] byteArray = System.Text.Encoding.Default.GetBytes...
- 5C# Primitive Datatypes - Bytes