Вопрос

I have the byte array of a string.

How to convert byte[] to String and String to byte[]

After compressing I get the compressed value in form of byte array. byte[] value at System.out is

h?ö\
É·ë’YO¸p˜à¼Œ[䔽"$žQºÍCïηfØzöÛBi¤!< 

How can I convert this byte array value to String keeping its value unchanged and then getting it back to byte array for decompressing? tried buitin functions but they change the whole compressed byte array

Это было полезно?

Решение 2

Use ISO-8859-1 when converting bytes to String and back

    String s = new String(bytes, "ISO-8859-1");
    bytes = s.getBytes("ISO-8859-1");

Другие советы

try new String(byteArray) and string.getBytes()

You need to specify the char set for your byte[] array while decoding from raw into string.

The String type contain such constructor String(bytes, Charset charset)

byte[] b=new byte[10];
b[0]=100;
b[1]=101;
b[2]=102;
b[5]=0;
b[6]=0;

String st=new String(b);
System.out.println(st);//def
b=st.getBytes();
for(int i=0;i<b.length;i++){
    System.out.println(b[i]);
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top