Results 1 to 7 of 7
- 02-17-2011, 10:12 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
A simple question about converting byte array to unicode string
Hi,
I have a bytearray which is coding for a unicode text ("BlaBla"). So my byte array is 12 bytes long (there are 6 characters in the text and 2 bytes for each character).
And here is the byte array:
66, 0, 108, 0, 97, 0, 66, 0, 108, 0, 97, 0
Everything seems to be normal but I can't convert this byte array back to "BlaBla" and show it in an text area. Instead of "BlaBla" I get some chinese like characters (䈀氀愀䈀氀愀BB䈀氀愀䈀氀愀).
I have tried so far:
String Message=new String(packageBytes,0,12, "UTF-16");
String Message=new String(packageBytes,0,12, "US-ASCII");
String Message=new String(packageBytes,0,12, "UTF-16BE");
String Message=new String(packageBytes,0,12, "UTF-16LE");
I have tried a lot of things and googled a lot but can't find a solution :confused:?
- 02-17-2011, 10:47 AM #2
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
Character Set Name for UniCode -->> "US-ASCII"
Try this -
String msg = new String(new byte[]{66, 0, 108, 0, 97, 0, 66, 0, 108, 0, 97, 0},0,12,"US-ASCII");
System.out.println(msg);
- 02-17-2011, 11:29 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-17-2011, 12:04 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
I can't put the output here because of the squares representing "0" bytes. 0's are being recognized as a character. Actually "66" and "0" together have to be a unicode character.
I get something like this as output:
[B, *, l, *, a, * ...]
12
*... is actually a square. Maybe I have to explain it better:
I am writing a java client which binds over a socket to a vb.net written server. Programs written with .Net together can exchange strings over network without a problem. But If I send the string to a java client, java client doesn't have a readString() method like .Net. So I decided to send data in byte array and convert it to string in java client. But then illegal characters are being a problem "ş" is then replaced with 2 other symbols in java side. So I decided to send data as unicode. Data is being delivered with 2 headers:
1 byte as "1" or "2" telling to other side what type of data is coming (same connection is going to be used for file transfer as well. 1 for text 2 for file chunks).
1 byte as length of data (in bytes) to be read (as x).
x bytes of data.
I really don't get why String(packageBytes,0,12, "UTF-16"); can create a string according to my byte array?Java Code:InputStream is = socket.getInputStream(); int packageType; int packageLen; packageType=is.read(); if (packageType==1) { packageLen=is.read(); System.out.println("Package Length: " + packageLen); byte[] packageBytes= new byte[packageLen]; is.read(packageBytes, 0, packageLen); int i; for(i=0;i<packageLen;i++){ System.out.println(packageBytes[i]);//just to verify bytes are stored in my byte array } String Message=new String(packageBytes,0,12, "UTF-16"); System.out.println(Message); ta.append(Message); //ta is a text area. Result is "B" (byte=66) so only the first character is written. I think that is because of the character with byte=0. }
- 02-17-2011, 12:09 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Check the CharSet class for the available encodings; it is a low byte first (little endian) UTF-16 encoding.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-17-2011, 12:47 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
"UTF-16LE" as charset worked thanks a lot!!!!
D But I have tried that as well. Interestingly it didn't work I think I forgot something. Thanks again...!
- 02-17-2011, 01:22 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Converting string to byte[]
By bobo67 in forum New To JavaReplies: 12Last Post: 09-10-2010, 09:10 PM -
converting byte array to bmp file
By Moorkh in forum New To JavaReplies: 2Last Post: 09-07-2010, 02:58 PM -
Need help converting int to a 4 byte array
By kook04 in forum Advanced JavaReplies: 5Last Post: 02-26-2010, 08:59 PM -
Converting Image to byte array[] ?
By afflictedd2 in forum CLDC and MIDPReplies: 0Last Post: 04-11-2009, 11:33 PM -
Byte arrays and MIDI - simple question?
By Ravaa in forum New To JavaReplies: 1Last Post: 03-23-2009, 09:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks