Results 1 to 15 of 15
  1. #1
    DeptOfMeteors is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default Expressing a byte string

    I need to know how to express a byte string literal in Java. I also need to compare it to a string of bytes that I'm reading from a file. I know how I'd do this in C. I'd have something like "\x000\x001\x002" (I think) then I'd then compare it with memcmp(). How do I do the same in Java?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    DeptOfMeteors is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    If I could find one, I'm sure I could figure out how to make one.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by DeptOfMeteors View Post
    If I could find one, I'm sure I could figure out how to make one.
    That's what I want to flag.

    In C/C++ you can convert byte array to string in a sense that human can understand. If the byte array contain some annoying characters, by converting each you can build a meaningful string.

    However Java doesn't have a direct way to do that.

  5. #5
    DeptOfMeteors is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    So, what you're saying is if I wanted to find out if the first byte in a byte array was 8, and the next was 3, and the next was 2, while I could do
    Java Code:
    if (memcmp("\x08\x03\x02", pByteArray, 3) == 0)
    in C, there isn't a one-liner in Java as simple that can accomplish this? Is the best I can do:
    Java Code:
    if (byteArray[0] == 8 && byteArray[1] == 3 && byteArray[2] == 2)
    ?

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    If you often have to check whether one array starts with elements equal to some other array you would write a method for that.

    The array you are comparing with might be expressed, reasonabley tersely, as

    Java Code:
    new byte[] {8, 3, 2}

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Quote Originally Posted by Eranga View Post
    So that method should have a way to convert values into a common format.

    I'm not sure what you mean. I had in mind something like:

    Java Code:
    boolean compare(byte[] arr, byte[] with) {
        if(arr.length < with.length) {
            return false;
        }
        for(int i = 0; i < with.length; i++) {
            if(arr[i] != with[i]) {
                return false;
            }
        }
        return true;
    }

    Usage would be like:

    Java Code:
    //if (memcmp("\x08\x03\x02", pByteArray, 3) == 0)
    if(compare(byteArray, new byte[] {8, 3, 2}))

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    That's in same type. I thought in the other array there are string literals, with the number in another array (byte array actually)

  13. #13
    DeptOfMeteors is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    Ok, now I've discovered another problem I'm going to have w/Java: no unsigned types. Quite literally, the line I have is:
    Java Code:
    private static byte[] _authenticationBytes = new byte[] { 0x01, 0x67, 0x22, 0x32, 0xa7, 0x0f, 0xef, 0x32, 0x65, 0x77, 0x62, 0x99, 0x88, 0x87, 0x11, 0x17 };
    and my parser has flagged all those bytes >0x7f as errors. The recommendation is that they be cast to bytes. You now know that what I want to do is compare a byte array with another byte array, and I'm just wondering what's going to happen w/the bytes that are cast. Will they become negatives, and the ones that are 0x88 will remain 0x88, (and then somehow be internally imaged as -120) or will something else happen to them?

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

    Default

    Quote Originally Posted by DeptOfMeteors View Post
    Ok, now I've discovered another problem I'm going to have w/Java: no unsigned types. Quite literally, the line I have is:
    Java Code:
    private static byte[] _authenticationBytes = new byte[] { 0x01, 0x67, 0x22, 0x32, 0xa7, 0x0f, 0xef, 0x32, 0x65, 0x77, 0x62, 0x99, 0x88, 0x87, 0x11, 0x17 };
    and my parser has flagged all those bytes >0x7f as errors. The recommendation is that they be cast to bytes. You now know that what I want to do is compare a byte array with another byte array, and I'm just wondering what's going to happen w/the bytes that are cast. Will they become negatives, and the ones that are 0x88 will remain 0x88, (and then somehow be internally imaged as -120) or will something else happen to them?


    No problem; a narrowing cast simply chops of the bytes that won't fit, so e.g -88 stays -88, whether it's an int or a byte. Values that won't fit in a narrower type will be mutilated though, e.g 1024 (an int) will be zero as a byte.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    DeptOfMeteors is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    That's the problem, I don't want these byte mutilated. So from what you are telling me, for all these bytes, the MSB will always be 0? So how do I express 88hex so that it'll be 10001000bin (notice the MSB set)?
    Java Code:
    -0x78
    ? (That's the right 2's complement representation, right?)

Similar Threads

  1. Converting string to byte[]
    By bobo67 in forum New To Java
    Replies: 12
    Last Post: 09-10-2010, 10:10 PM
  2. String byte array
    By myka in forum Advanced Java
    Replies: 2
    Last Post: 03-18-2010, 03:33 AM
  3. String from byte array
    By justint in forum New To Java
    Replies: 2
    Last Post: 01-22-2010, 07:58 AM
  4. Search a string in a byte array
    By 2BOrNot2B in forum New To Java
    Replies: 0
    Last Post: 03-12-2009, 06:52 PM
  5. String byte storage
    By bozovilla in forum New To Java
    Replies: 1
    Last Post: 11-24-2007, 07:35 AM

Posting Permissions

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