-2

I convert a byte array msg into a string encodedmsg withe the formatting ISO-8859-1 like this:
String encodedmsg = new String(msg, "ISO-8859-1");
I cant work out how to now go from my string encodedmsg back to the byte array msg, essentially revert what I do in the first place. Any help would be much appreaciated :)

oguz ismail
  • 1
  • 16
  • 47
  • 69

2 Answers2

2

You can get back byte[] as follows:

byte[] arr = encodedmsg.getBytes(Charset.forName("ISO-8859-1"));

Demo:

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) throws UnsupportedEncodingException {
        byte[] msg = "hello".getBytes();
        System.out.println(Arrays.toString(msg));
        String encodedmsg = new String(msg, "ISO-8859-1");
        System.out.println(encodedmsg);
        byte[] arr = encodedmsg.getBytes(Charset.forName("ISO-8859-1"));
        System.out.println(Arrays.toString(arr));
    }
}

Output:

[104, 101, 108, 108, 111]
hello
[104, 101, 108, 108, 111]
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1
new String(encodedmsg.getBytes("ISO-8859-1"));
Piaget Hadzizi
  • 702
  • 8
  • 15