|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Java encoding...
Hello,
I need to write a byte stream onto the network, what i plan to do is take the byte arrays i have, convert them to a string and recreate the byte array using the string i receive on the other end. Let me illustrate my problem... 1. bss is what i wish to transfer byte bss[] = new byte[1]; bss[0] = (byte) (129 & 0xff); System.out.println("Byte value: " + bss[0]); System.out.println("Int value: " + ( bss[0] & 0xff )); 2. convert bss to a string StringBuffer ston = new StringBuffer(); ston.append(new String(bss,"UTF-8")); 3. Recreate bss from string System.out.println(ston.toString()); byte bss2[] = ston.toString().getBytes(); System.out.println("Byte value: " + bss2[0]); System.out.println("Int value: " + ( bss2[0] & 0xff )); For some reason my output is:- Byte value: -127 Int value: 129 ? Byte value: 63 Int value: 63 I need the same byte value and int value after recreating the bytes using getBytes(), i think there is some issue with the encoding issues coming up, wondering if anyone knows a fix for what i'm trying to do.. Thanks Neville |
|
#2
|
|||
|
|||
|
hi
i'm not really sure,but which jdk do u use????if u use jdk6 , u have to go with somthing new,generics and collections.
|
|
#3
|
|||
|
|||
|
Hi.
If I understand it well, than you are sending 129 which is 10000001 in binary form. From what I read on Wikipedia on UTF-8 I think that if the number starts 1 it means that the letter is saved in 2 numbers. The documentation on String(byte[] bytes, String charsetName) says: Quote:
So I think that you are getting this unspecified behavior. See the charset and try to send for example number 10. If it works, than you have better use different charset. UTF-8 has too many rules for your case. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Java encoding... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|