Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 14th, 2008, 05:23 PM
nevillemehta nevillemehta is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: India
Posts: 64 nevillemehta User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 44 m 17 sec
Reputation Power: 6
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

Reply With Quote
  #2  
Old April 17th, 2008, 06:34 AM
eejimkos eejimkos is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 4 eejimkos User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 40 sec
Reputation Power: 0
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.

Reply With Quote
  #3  
Old September 6th, 2008, 03:29 AM
vvharmp vvharmp is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2008
Posts: 2 vvharmp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 3 m 49 sec
Reputation Power: 0
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:
The behavior of this constructor when the given bytes are not valid in the given charset is unspecified.

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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Java encoding...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT