//string转byte[]:
byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str );

//byte[]转string:
string str = System.Text.Encoding.Default.GetString ( byteArray );

//string转ASCII byte[]:
byte[] byteArray = System.Text.Encoding.ASCII.GetBytes ( str );

//ASCII byte[]转string:
string str = System.Text.Encoding.ASCII.GetString ( byteArray );

Java中字符串string和字节数组byte[]的转换

//string 转 byte[]

String str = "Hello";

byte[] srtbyte = str.getBytes();

// byte[] 转 string

String res = new String(srtbyte);

System.out.println(res);


//当然还有可以设定编码方式
的

String str = "hello";

byte[] srtbyte = null;

try {

    srtbyte = str.getBytes("UTF-8");

    String res = new String(srtbyte,"UTF-8");

    System.out.println(res);

    } catch (UnsupportedEncodingException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

 

相关文章:

  • 2022-02-23
  • 2021-12-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-25
  • 2021-05-29
  • 2022-12-23
猜你喜欢
  • 2021-07-19
  • 2022-12-23
  • 2021-08-12
  • 2022-03-08
  • 2021-09-29
  • 2022-02-08
  • 2022-12-23
相关资源
相似解决方案