java字符串转byte 怎样将Byte转换成字符串?

怎样将Byte转换成字符串?有两种方法:方法1://string to bytestring stringmessage=“你好吗?" 控制台写入线(“{0}”,StringMessage)Syste

怎样将Byte转换成字符串?

有两种方法:

方法1:

//string to byte

string stringmessage=“你好吗?"

控制台写入线(“{0}”,StringMessage)System.Text.ASCIIEncoding系统ASCII=新System.Text.ASCIIEncoding系统()

字节[]字节消息=ASCII.GetBytes(stringmessage)

//字节到字符串

字节[]字节消息System.Text.ASCIIEncoding系统ASCII=新System.Text.ASCIIEncoding系统()

字符串消息=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字节到字符串

byte[]BytesMessageSystem.Text.utf8编码UTF8=新System.Text.utf8编码()

怎么将字符串转换为byte数组?

1。字符串到字节[]字符串STR=“你好”字节[]srtbyte=字符串getBytes()

2,byte[]to string

byte[]srtbytestring res=新字符串(srtbyte)系统输出打印(分辨率)

3。设置编码模式转换

字符串STR=“Hello”byte[]srtbyte=nulltry{srtbyte=字符串getBytes(“UTF-8”)String res=新字符串(srtbyte,“UTF-8”)系统输出打印(res)}catch(UnsupportedEncodingException e){//TODO自动生成的catch块e.printStackTrace()}