Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How do I convert byte[] to String?

807591Jun 14 2008 — edited Jun 16 2008
I've got a class that pulls information from a GPS receiver on a COM port. That works fine, but the information I'm getting from the port is in the following format:
Reading serial port...
44,44,52,46,50,44,77,44,45,51,52,46,50,44,77,44,44,48,48,48,48,42,120,30,-104,-128,96,120,96,0,-26,126,-98,-104,-98,0,30,-122,6,30,-122,6,120,-122,-32,-122,-32,-122,-32,-122,-32,-122,-32,-122,-32,-122,-32,-122,-32,-122,-32,-122,-32,120,-122,-32,-122,-32,-122,-32,-122,-104,-122,6,120,6,-98,30,-104,-128,96,120,96,0,-26,24,-26,-26,-104,30,30,-122,24,120,-32,120,30,102,120,-26,120,-26,-4,48,50,52,44,86,44,52,48,53,54,46,49,50,50,49,44,78,44,48,55,51,53,52,38,54,70,70,-58,118,-115,25,25,89,70,6,102,6,-122,-58,-58,-58,-26,83,102,-122,
-61,-31,
36,71,80,71,71,65,44,50,49,52,53,53,54,46,48,50,52,44,52,48,53,54,46,49,50,50,49,44,78,44,48,55,51,53,52,46,50,51,52,52,44,87,44,48,44,48,48,44,44,52,46,50,44,77,44,45,51,52,46,50,44,77,44,44,48,48,48,48,42,52,66,13,10,36,71,80,71,83,
65,
So it's pretty obvious that it's spitting out ASCII values, so I changed the code a bit to print out the corresponding letters instead:
Reading serial port...
$GPGGA,214724.031,4056.1221,N,07354.2344,W,0,00,,4.2,M,-34.2,M,,0000*48
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPGSV,3,1,11,31,70,286,,14,57,089,,32,51,293,,30,44,061,*7D
$GPGSV,3,2,11,05,24,046,,22,23,167,,20,20,315,,16,15,194,*70
$GPGSV,3,3,11,12,12,039,,29,09,103,,11,00,279,*4F
$GPRMC,214724.031,V,4056.1221,N,07354.2344,W,,,140608,,,N*68
$GPGGA,214725.031,4056.1221,N,07354.2344,W,0,00,,4.2,M,-34.2,M,,0000*49
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
Now those are the NMEA sentences that the GPS receiver spits out, that's exactly what I need. Only problem is I'm printing them out using the following code:
      byte[] buffer = new byte[4096];
      InputStream theInput = thePort.getInputStream();
      System.out.println("Reading serial port...");
      while (canRead()) // Loop
      {
        int bytesRead = theInput.read(buffer);
        if (bytesRead == -1)
          break;
        for (int z = 0; z < bytesRead; z++)
        {    	
        	System.out.print(Character.toString((char)buffer[z]));
        }
So as you can see, it's simply taking the byte, converting it from ASCII to a symbol or letter, and putting it on the screen. Then it moves onto the next byte. So what I've tried to do is change it again so that it adds each converted letter to a string value, starting a completely new string every time it hits a $ sign, which always starts a NMEA sentence:
      byte[] buffer = new byte[4096];
      InputStream theInput = thePort.getInputStream();
      System.out.println("Reading serial port...");
      while (canRead()) // Loop
      {
        int bytesRead = theInput.read(buffer);
        if (bytesRead == -1)
          break;
        String s = "";
        for (int z = 0; z < bytesRead; z++)
        {
        	if (Character.toString((char)buffer[z]).equalsIgnoreCase("$") && s.length() > 1)
        	{
        		System.out.println(s);
        		s = Character.toString((char)buffer[z]);
        	}
        	else
        		s = s + Character.toString((char)buffer[z]);

//        	System.out.print(Character.toString((char)buffer[z]));
        }
s = "";
}
The problem with this is for some reason the sentences aren't whole, there's line breaks inserted into them. So when I'm passing them to the parser, I'm passing incomplete sentences. Here's the output I'm getting now:
Reading serial port...
$GPGSA,A,1,,,,,,,,,,,,,,,*1E

.2,M,-34.2,M,,0000*4D

$GPGSA,A,1,,,,,,,,,,,,,,,*1E

,32,51,293,,30,44,061,*7D


$GPGSV,3,3,11,12,12,039,,29,09,103,,11,00,279,*4F
How do I get each NMEA sentence into a string so I can send it to a parser? Any ideas?

Edited by: DeX on Jun 14, 2008 6:02 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 14 2008
Added on Jun 14 2008
5 comments
330 views