Convert a String to a byte array and then back to the original String

后端 未结 4 2133
一整个雨季
一整个雨季 2020-12-01 07:33

Is it possible to convert a string to byte array and then convert it back to the original string in Java or Android?

My objective is to send some strings to a microc

4条回答
  •  一向
    一向 (楼主)
    2020-12-01 07:51

    You can do it like this.

    String to byte array

    String stringToConvert = "This String is 76 characters long and will be converted to an array of bytes";
    byte[] theByteArray = stringToConvert.getBytes();
    

    http://www.javadb.com/convert-string-to-byte-array

    Byte array to String

    byte[] byteArray = new byte[] {87, 79, 87, 46, 46, 46};
    String value = new String(byteArray);
    

    http://www.javadb.com/convert-byte-array-to-string

提交回复
热议问题