Results 1 to 4 of 4
  1. #1
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Removing thr last two bytes from string

    Hi
    I want to split a string in two. I want the last two bytes to be in one string and the rest in an other. The problem is I don't know the length of the string.

    Im collecting data from a serial inputstream and the last two bytes are CRC. I need to control that crc is correct befor I handle the data in the stream.

    Will some thing like this work :
    Java Code:
    input.getLength(i);
    input.split(i - 2);
    Or am I far off?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Removing thr last two bytes from string

    Quote Originally Posted by KarlNorway View Post
    Will some thing like this work :
    Java Code:
    input.getLength(i);
    input.split(i - 2);
    Or am I far off?
    Sort of; can the String be shorter than two characters? Have you read the API documentation for the String class?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Posts
    57
    Rep Power
    0

    Default Re: Removing thr last two bytes from string

    Thanks for fast answer Jos

    The shortest the string can be is 15 bytes. The crc is on 6 bytes in the message.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Removing thr last two bytes from string

    Quote Originally Posted by KarlNorway View Post
    Thanks for fast answer Jos

    The shortest the string can be is 15 bytes. The crc is on 6 bytes in the message.
    Ok, now read the API for the methods in the String class; pay special attention to the length() method and the substring( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Removing All Commas from a String
    By andy9507 in forum New To Java
    Replies: 6
    Last Post: 05-12-2011, 04:16 AM
  2. Removing words from a String
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 03-15-2011, 10:50 AM
  3. Removing last character from a String
    By newToJava3 in forum New To Java
    Replies: 2
    Last Post: 11-28-2010, 12:43 AM
  4. Replies: 1
    Last Post: 04-10-2010, 04:37 PM
  5. Replies: 1
    Last Post: 01-11-2008, 10:06 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
  •