java字符串轉(zhuǎn)byte 怎樣將Byte轉(zhuǎn)換成字符串?
怎樣將Byte轉(zhuǎn)換成字符串?有兩種方法:方法1://string to bytestring stringmessage=“你好嗎?" 控制臺寫入線(“{0}”,StringMessage)Syste
怎樣將Byte轉(zhuǎn)換成字符串?
有兩種方法:
方法1:
//string to byte
string stringmessage=“你好嗎?"
控制臺寫入線(“{0}”,StringMessage)System.Text.ASCIIEncoding系統(tǒng)ASCII=新System.Text.ASCIIEncoding系統(tǒng)()
字節(jié)[]字節(jié)消息=ASCII.GetBytes(stringmessage)
//字節(jié)到字符串
字節(jié)[]字節(jié)消息System.Text.ASCIIEncoding系統(tǒng)ASCII=新System.Text.ASCIIEncoding系統(tǒng)()
字符串消息=ASCII.GetString(bytesmessage)
方法2:
//string to UTF-8 byte
string stringmessage=“你好,你好嗎?Pi/u03c0日元/uffe5“System.Text.utf8編碼UTF8=新System.Text.utf8編碼()
byte[]bytesmessage=utf8。GetBytes(stringmessage)
//UTF-8字節(jié)到字符串
byte[]BytesMessageSystem.Text.utf8編碼UTF8=新System.Text.utf8編碼()
怎么將字符串轉(zhuǎn)換為byte數(shù)組?
1。字符串到字節(jié)[]字符串STR=“你好”字節(jié)[]srtbyte=字符串getBytes()
2,byte[]to string
byte[]srtbytestring res=新字符串(srtbyte)系統(tǒng)輸出打?。ǚ直媛剩?/p>
3。設(shè)置編碼模式轉(zhuǎn)換
字符串STR=“Hello”byte[]srtbyte=nulltry{srtbyte=字符串getBytes(“UTF-8”)String res=新字符串(srtbyte,“UTF-8”)系統(tǒng)輸出打?。╮es)}catch(UnsupportedEncodingException e){//TODO自動生成的catch塊e.printStackTrace()}