문제

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