Click to See Complete Forum and Search --> : Conversion from byte array to String and back


FABIO MARTINEZ
May 12th, 2000, 09:43 AM
Hi, I'd be very grateful if somebody help.
I need to convert an array of bytes to a String, sending this string as a parameter and getting again a byte array.
The first step is acomplished through new String(byte[] myarray). Then I'm using getbytes() method.
This scheme works ok if bytes containing 0x8d, 0x8e, 0x8f, 0x90, 0x9d, 0x9e, are not involved. If it's not the case in my returned bytearray I obtain 0x3f.

Here is a demonstrating piece of code. Thanks.

javacode/
public static void main(String[] args) {

byte [] byteArray = new byte[256];
for (int i = 0; i < 256; i++) {
byteArray[i] = (byte)i;
}

String kk = new String(byteArray);
byte [] byteArray2 = kk.getBytes();

for (int j = 0; j < 256; j++) {
System.out.println(byteArray[j] + " = " + byteArray2[j]);
}
/javacode

Mr Bump
February 14th, 2001, 04:19 AM
Afraid I can't help you, but I've just hit exactly the same problem (also occurs with 0x81).

did you find a reason and/or a solution ?

fabio.martinez
February 14th, 2001, 05:41 AM
Yes I've found a workaround to this problem: use the codepage ISO8859_1.
With the default codepage some values like 0x81 has no representation so you obtain the error. With the korean codepage every number you can obtain when obtaining a byte array from a string has representation so you can smoothly perform your string-byte-string tasks.

esjayes
October 3rd, 2001, 03:52 PM
How do I specify the codepage? I am using Visual Cafe 4.1.
Please advise. Thanks.