Results 1 to 6 of 6

Thread: char problem.

  1. #1
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Wink char problem.

    Okey I'll keep it simple... I have a encoding problem. Here is what im doing:
    Java Code:
    private  InputStream instrom; // make space for InputStream.
    instrom = sPort.getInputStream(); // get stream from serialport.
    // I add a eventlistener to the port 
    sPort.addEventListener(this);
    
    
    public void serialEvent(SerialPortEvent e){ // eventlistener
    
     switch (e.getEventType()){ // switch on different events
         case SerialPortEvent.DATA_AVAILABLE:  // if data availeble event
    
                 boolean done = false;
                 boolean gotsentence = false;
               
                 while(!done){    // looping for as long as the sentence is not complete
    
                     try{
    
                         nyData = instrom.read(); // read and put it in a INT nyData
    
    
                         switch(nyData){ // depening on what nydata gives switch
    
                             case -1:
                                 done = true;
                                 break;
    
    
                             case 0x0a:
                                 gotsentence = true;
                                 done = true;
                                 break;
    
    
    
                         }
                    
    
                         NMEAstring += Character.toString((char)nyData); // get char at int to string 
                         
    
                     }catch (Exception ef){ }
                 }
                 if(gotsentence){
                     System.out.println("NMEA Sentence\t[" + NMEAstring + "]\n");
                     NMEAstring = "";  // reset string
                 }
    
             break;
         case SerialPortEvent.BI:
    
             }
    
    }
    Im getting an encoding problem with squares insted of numbers/didgets/signs... so how can i change encoding in a EASY way (noob) =P Thanks for replys and thoughts =)

  2. #2
    Debugger is offline Member
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    i believe there is a way to set the encoding type..Not 100% on that but i was reading about similar issues for a project im working on.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,365
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by santa View Post
    Java Code:
    NMEAstring += Character.toString((char)nyData); // get char at int to string
    Im getting an encoding problem with squares insted of numbers/didgets/signs... so how can i change encoding in a EASY way (noob) =P Thanks for replys and thoughts =)
    You don't do any decoding at all; you assume that every byte from the InputStream represents a single char. Is the stream encoded in any form at all? If so you should collect the bytes and do the decoding afterwards. If there is no encoding present, is the data in some binary form? If so you can't represent it like character data the way you do it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default

    Hi i do get strings with numbers and letters .... all I do is what i showed you ... my only problem is that i get one or two squares in every sentence. But its still data loss =P could you guide me through the process? StringBuffer maybe ? Thank you for reply =)

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,365
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by santa View Post
    Hi i do get strings with numbers and letters .... all I do is what i showed you ... my only problem is that i get one or two squares in every sentence. But its still data loss =P could you guide me through the process? StringBuffer maybe ? Thank you for reply =)
    Well, for one thing, you're adding those byte -1 values (if sent by that thing on the other side of the wire) to your String and they end up there as character Unicode value 0xffff.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default

    Well, for one thing, you're adding those byte -1 values (if sent by that thing on the other side of the wire) to your String and they end up there as character Unicode value 0xffff.
    I love you man ... you solved all my problems =)

Similar Threads

  1. check a string char by char
    By Sotsiak in forum New To Java
    Replies: 2
    Last Post: 10-23-2010, 09:24 PM
  2. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 05:48 AM
  3. JNA char **
    By H_P in forum Advanced Java
    Replies: 0
    Last Post: 01-14-2010, 11:13 PM
  4. Problem with if statement - Char
    By KardKaper in forum New To Java
    Replies: 8
    Last Post: 12-22-2009, 01:14 PM
  5. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 12:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •