How to convert String to byte without changing?

前端 未结 4 832
鱼传尺愫
鱼传尺愫 2021-01-29 07:28

I need a solution to convert String to byte array without changing like this:

Input:

 String s=\"Test\";

Output:

String         


        
4条回答
  •  情话喂你
    2021-01-29 08:20

    In general that's probably not what you want to do, unless you're serializing or transmitting the data. Also, Java strings are UTF-16 rather than UTF-8, which what more like what you're expecting. If you really do want/need this then this should work:

    String str = "Test";
    byte[] raw = str.getBytes(new Charset("UTF-8", null));
    

提交回复
热议问题