Home > Back-end >  Android 2.1 (API 7) String to bytesArray java.lang.NoSuchMethodError: java.lang.String.getBytes
Android 2.1 (API 7) String to bytesArray java.lang.NoSuchMethodError: java.lang.String.getBytes

Time:01-28

In API 7 when try

val string = "string"
val bytes = string.toByteArray(Charset.forName("UTF-8"))

i have exception

java.lang.NoSuchMethodError: java.lang.String.getBytes

Is any way to get bytes from string without standard method? Looks like, convertation string to byte array starts with Api 8.

CodePudding user response:

There are versions of getBytes() on String that work back to API Level 1, such as this one.

CodePudding user response:

ChatGPT help me :)

String string = "your_string";
ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(string);
byte[] byteArray = byteBuffer.array();
  • Related