How to convert a String array to a Byte array? (java) How to convert a String array to a Byte array? (java) arrays arrays

How to convert a String array to a Byte array? (java)


Array to Array you should convert manually with parsing into both sides, but if you have just a String you can String.getBytes() and new String(byte[] data);like this

public static void main(String[] args) {    String[] strings = new String[]{"first", "second"};    System.out.println(Arrays.toString(strings));    byte[][] byteStrings = convertToBytes(strings);    strings = convertToStrings(byteStrings);    System.out.println(Arrays.toString(strings));}private static String[] convertToStrings(byte[][] byteStrings) {    String[] data = new String[byteStrings.length];    for (int i = 0; i < byteStrings.length; i++) {        data[i] = new String(byteStrings[i], Charset.defaultCharset());    }    return data;}private static byte[][] convertToBytes(String[] strings) {    byte[][] data = new byte[strings.length][];    for (int i = 0; i < strings.length; i++) {        String string = strings[i];        data[i] = string.getBytes(Charset.defaultCharset()); // you can chose charset    }    return data;}

for one byte[] from string[] you have to:

  • to byteArray concat byte arrays from each string using some delimeter
  • from bytearray split by te same delimiter and create String as I described above.


You don't say what you want to do with the bytes (aside from convert them back to a String[] afterward), but assuming you can just treat them as an opaque bag of data (so you can save them to a file or send them over the network or whatnot, but you don't need to examine or modify them in any way), I think your best bet is to use serialization. To serialize your string-array, you would write something like:

final String[] stringArray = { "foo", "bar", "baz" };final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();final ObjectOutputStream objectOutputStream =    new ObjectOutputStream(byteArrayOutputStream);objectOutputStream.writeObject(stringArray);objectOutputStream.flush();objectOutputStream.close();final byte[] byteArray = byteArrayOutputStream.toByteArray();

and to recover it afterward, you'd write the reverse:

final ByteArrayInputStream byteArrayInputStream =    new ByteArrayInputStream(byteArray);final ObjectInputStream objectInputStream =    new ObjectInputStream(byteArrayInputStream);final String[] stringArray2 = (String[]) objectInputStream.readObject();objectInputStream.close();


You can check this

package javaapplication2;import java.util.Arrays;/** * * @author Ali */public class JavaApplication2 { public static byte[] to_byte(String[] strs) {        byte[] bytes=new byte[strs.length];        for (int i=0; i<strs.length; i++) {            bytes[i]=Byte.parseByte(strs[i]);        }        return bytes;    }    public static void main(String[] args) {        String[] input = {"1","2","3"};       //original data        byte[] byteArray = to_byte(input);//data to byte array        String[] recovered=Arrays.toString( byteArray).split(",");// recovered data     }    }