Results 1 to 19 of 19
Thread: Byte [] to String
- 12-19-2012, 09:19 AM #1
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
Byte [] to String
Hello Everyone,
Well obviously I have a problem :
I'm reading bytes coming from a serial port and I want to show these bytes like strings well it does show but there's some letter that 're incomprehensible.
This is my code :
So is there another way around ? and thanks .Java Code:byte [] readbuffer = new byte [20]; try { while (inputstream.available () > 0 ) { int nymBytes = inputstream.read(readbuffer); } String tr = new String (readbuffer,"US-ASCII"); System.out.println(tr);
- 12-19-2012, 11:21 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Byte [] to String
What is the result that you expect from the code and what you get at the moment?
In your snippet you are likely to print out the last data in the buffer.Last edited by wsaryada; 12-19-2012 at 11:27 AM.
Website: Learn Java by Examples
- 12-19-2012, 11:32 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Byte [] to String
Don't use the available() method.
Use:
since read(buffer) returns the number of bytes read, and -1 means EOS.Java Code:int numBytes = 0; while ((numBytes = inputStream.read(readbuffer)) > -1) { }
Also, as wsaryada suggests, your current code only does the last 20 (or less) bytes.
You want to outputting as you loop.
Note, you want to only output the number of bytes read...anything more is just going to be garbage.Please do not ask for code as refusal often offends.
- 12-19-2012, 12:00 PM #4
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
Re: Byte [] to String
thanks for the quick response.
What I expect is showing the traffic coming from the serial port and I know that 're letters & numbers.
what's sent it's showed but with uncomprehensible letters (small rectangles).
- 12-19-2012, 12:08 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
Re: Byte [] to String
so I changed it like u said :
what I get is : [B@1296d1dJava Code:int numBytes; byte [] readbuffer = new byte [20]; try { String tr = new String (readbuffer,"US-ASCII"); while ((numBytes = inputstream.read(readbuffer)) > -1 ) { tr =readbuffer.toString(); System.out.println(tr); TrafficSerialPort.Traffic.setText(tr); } }catch (IOException e) {System.out.println(e);}
But I want actual words!
- 12-19-2012, 12:28 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Byte [] to String
First, you need to use the String constructor that takes an offset and length, as well as the buffer and charset.
That's because you will not always have 20 bytes used in your buffer.
The numBytes variable is very important.
Second you need to do this inside the loop.
At the moment you are doing the conversion outside the loop, and with an empty buffer.
Your:
is simply using the default Object toString, which is the memory address of the object in question.Java Code:tr = readbuffer.toString();
It is not doing any sort of translation of the byte[].
Finally, do you really want to be updating the Traffic field everytime around this loop?Please do not ask for code as refusal often offends.
- 12-19-2012, 12:37 PM #7
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
Re: Byte [] to String
Ok now I'm back to start point : this is my code now :
Now it shows me the Traffic with letters & all but with some garbage (small rectangles) and that's what I want to dispose off.Java Code:int numBytes; byte [] readbuffer = new byte [20]; try { while ((numBytes = inputstream.read(readbuffer)) > -1 ) { String tr = new String (readbuffer,"US-ASCII"); System.out.println(tr); TrafficSerialPort.Traffic.append(tr); } }catch (IOException e) {System.out.println(e);}
yes cause the traffic is always active & changing so I need to capture the Traffic and show it.
- 12-19-2012, 12:58 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Byte [] to String
To quote myself:
"use the String constructor that takes an offset and length"
You are assuming your buffer is filled each time.
This is not necessarily the case.
And if, when that is sorted, you still get "garbage" then you need to check whether the data you are receiving is actually in US-ASCII.Please do not ask for code as refusal often offends.
- 12-19-2012, 01:30 PM #9
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: Byte [] to String
You will not succeed reading from a stream that way. As long as the stream is open there might be more input, so you need to append the input to another buffer and decide at whcih point you want to show your content to the user. While using println() will succeed as it is also a stream most other uses will not.
When you have your buffer filled you may split that content up according to your serial separation characters/synchronization bytes, etc. You should make yourself familiar with serial communication in that respect.
In general: It is not a good idea to say that "with letters & all but with some garbage" is a problem, but to show the example. E.g. the [B@1296d1d is a direct result from your wrong usage of toString and not from the serial port. So try to keep your description with as much useful information as possible.I like likes!.gif)
- 12-19-2012, 01:44 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Byte [] to String
I assumed the append() call on whatever Traffic is is where they'r ebuilding up the response?
Have I missed something here?Please do not ask for code as refusal often offends.
- 12-19-2012, 03:01 PM #11
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: Byte [] to String
I think you are right, I was still with the first two versions he presented in this and the other thread I guess... maybe I am getting too old... ^^
However he will have to go through his input byte by byte that way to eliminate unwanted characters... or use RegEx...?Last edited by Sierra; 12-19-2012 at 03:04 PM.
I like likes!.gif)
- 12-19-2012, 03:12 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Byte [] to String
Well, at the moment they're not writing the buffer correctly anyway.
And I wouldn't be surprised to find out that it is not a US-ASCII stream coming in.Please do not ask for code as refusal often offends.
- 12-19-2012, 03:38 PM #13
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
Re: Byte [] to String
well this is how I handle the events on the serial port so I can read the traffic(Actually my traffic is a telnet traffic over serial port and i'm using a computer on the other end to send text to this one (just to try my program) ) :
Code stripGarbage (); :Java Code:public void serialEvent(SerialPortEvent event) { //gestion des événements sur le port : //on ne fait rien sauf quand les données sont disponibles switch (event.getEventType()) { case SerialPortEvent.BI : case SerialPortEvent.OE : case SerialPortEvent.FE : case SerialPortEvent.PE : case SerialPortEvent.CD : case SerialPortEvent.CTS : case SerialPortEvent.DSR : case SerialPortEvent.RI : case SerialPortEvent.OUTPUT_BUFFER_EMPTY : break; case SerialPortEvent.DATA_AVAILABLE : int numBytes; byte [] readbuffer = new byte [20]; try { while ((numBytes = inputstream.read(readbuffer)) > -1 ) { String tr = new String (readbuffer); System.out.println(tr); TrafficSerialPort.Traffic.append(stripGarbage(tr)); } }catch (IOException e) {System.out.println(e);} break; } }
is this the way the best way?Java Code:private String stripGarbage(String s) { StringBuilder sb = new StringBuilder(s.length()); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) { sb.append(c); } } return sb.toString(); }
and the other thing is : when I'm reading the traffic some of words came unfinished. this is a portion from the result : Hello...123...Hello...123..3llo.Hello...123
and thanks a lot for the responses.
- 12-19-2012, 03:54 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Byte [] to String
You're still assuming the whole buffer is being populated.
Until you actually fix that problem you will continue to get incorrect output.
I have said this several times now, and am beginning to get bored of it...Please do not ask for code as refusal often offends.
- 12-19-2012, 04:03 PM #15
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
Re: Byte [] to String
ok i'll try and get back to you thanks and sorry for the inconvinience.
- 12-19-2012, 04:58 PM #16
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
Re: Byte [] to String
So here's my solution to this problem : now I wait for the buffer to be filled then I read it.well this solution works for me but it's too slow
Java Code:int numBytes; byte [] readbuffer = new byte [20]; try { try { while ((numBytes = inputstream.read(readbuffer)) > -1 ) { Thread.sleep(1500); String tr = new String(readbuffer,0,20,"ISO-8859-1"); System.out.println(tr); TrafficSerialPort.Traffic.append(stripGarbage(tr)); } }catch (IOException e) {System.out.println(e);} } catch (InterruptedException e) { System.out.println("Interrupted"); break;
- 12-19-2012, 05:01 PM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Byte [] to String
No!
numBytes (as I said before) is the number of bytes read into the buffer.
Use it!
String tr = new String(readbuffer,0,numBytes,"ISO-8859-1");
Get rid of the thread sleep...Please do not ask for code as refusal often offends.
- 12-19-2012, 08:06 PM #18
Re: Byte [] to String
You can take a horse to the water...
Why do they call it rush hour when nothing moves? - Robin Williams
- 12-20-2012, 09:03 AM #19
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
Similar Threads
-
Byte encapsulation in String
By ozpenstillson in forum Advanced JavaReplies: 4Last Post: 06-29-2012, 06:42 PM -
Byte[] to string and string to byte[] - for database
By kronox in forum New To JavaReplies: 2Last Post: 11-21-2011, 12:08 AM -
Expressing a byte string
By DeptOfMeteors in forum New To JavaReplies: 14Last Post: 11-25-2010, 10:47 PM -
String from byte array
By justint in forum New To JavaReplies: 2Last Post: 01-22-2010, 06:58 AM -
String byte storage
By bozovilla in forum New To JavaReplies: 1Last Post: 11-24-2007, 06:35 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote-.gif)
but thanks to you and untill next time .

Bookmarks