Check for 0xff in byte array

Discussion in 'Java' started by Steve Rainbird, Jul 16, 2008.

  1. I need to check if there is a 0xff in a particular byte of a byte array
    (actually in 2 consecutive bytes) which represents a trailer of a file.

    I can't work out how to do it.

    Any help would be appreciated.

    TIA

    --
    Steve
     
    Steve Rainbird, Jul 16, 2008
    #1
    1. Advertising

  2. Steve Rainbird wrote:
    > I need to check if there is a 0xff in a particular byte of a byte array
    > (actually in 2 consecutive bytes) which represents a trailer of a file.
    >
    > I can't work out how to do it.
    >
    > Any help would be appreciated.
    >
    > TIA
    >


    assume byte[] b

    if (b[n] == (byte)0xff)
    // true

    --

    Knute Johnson
    email s/nospam/knute2008/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
     
    Knute Johnson, Jul 16, 2008
    #2
    1. Advertising

  3. Steve Rainbird

    Mark Space Guest

    Knute Johnson wrote:
    > Steve Rainbird wrote:
    >> I need to check if there is a 0xff in a particular byte of a byte
    >> array (actually in 2 consecutive bytes) which represents a trailer of
    >> a file.
    >>
    >> I can't work out how to do it.
    >>
    >> Any help would be appreciated.
    >>
    >> TIA
    >>

    >
    > assume byte[] b
    >
    > if (b[n] == (byte)0xff)
    > // true
    >


    Another possibility might be:

    public final static byte TRAILER = -1; // 0xff

    //...

    if( b == TRAILER ) {
    // true
    }

    Which I think avoids the conversion to int, which can be expensive.
     
    Mark Space, Jul 17, 2008
    #3
  4. Steve Rainbird

    Mark Space Guest

    Peter Duniho wrote:
    > On Wed, 16 Jul 2008 17:11:23 -0700, Mark Space
    > <> wrote:
    >
    >> [...]
    >> public final static byte TRAILER = -1; // 0xff
    >>
    >> //...
    >>
    >> if( b == TRAILER ) {
    >> // true
    >> }
    >>
    >> Which I think avoids the conversion to int, which can be expensive.

    >
    > I'm in favor of using a constant simply because I don't like unnamed
    > magic numbers in my code. But I'm curious what conversion to int you
    > are saying might be avoided? If "b" is a byte[], then "b[n]" is a byte
    > and so no conversion should be necessary to compare "(byte)0xff" (which
    > is also a byte, at compile-time) to "b[n]".
    >
    > Pete


    First, your right, I feebed. I thought I read that Knute was casting to
    int, not byte. My bad.

    However, I've had problems with performance with bytes before, so I did
    some checking before posting this. I don't see any JVM byte code that
    compares bytes. Just ints, doubles, longs and references. So I think
    both Knutes and my version will convert both bytes to ints before doing
    the compare.

    Might as well do

    if( (int)b[n] == -1 )

    and get it over with. There's only one conversion there, not two.
    Though I think this might be a lot harder to read than the other two
    versions.
     
    Mark Space, Jul 17, 2008
    #4
  5. Mark Space wrote:
    > However, I've had problems with performance with bytes before, so I did
    > some checking before posting this. I don't see any JVM byte code that
    > compares bytes. Just ints, doubles, longs and references. So I think
    > both Knutes and my version will convert both bytes to ints before doing
    > the compare.


    It's been a while since I last played with the bytecode, but from what I
    recall, a byte-to-int conversion is free while an int-to-byte is not.

    Certainly, it goes without mention that the decision of making bytes
    signed was a poor one.
    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Jul 17, 2008
    #5
  6. Steve Rainbird

    Mark Space Guest

    Joshua Cranmer wrote:

    >
    > It's been a while since I last played with the bytecode, but from what I
    > recall, a byte-to-int conversion is free while an int-to-byte is not.



    That might be true. The calculations I was testing converted bytes to
    ints, then back again. So the second operation might have been what was
    causing the slower processing.
     
    Mark Space, Jul 17, 2008
    #6
  7. "Knute Johnson" <> wrote in message
    news:487e7733$0$4069$...
    > Steve Rainbird wrote:
    >> I need to check if there is a 0xff in a particular byte of a byte array
    >> (actually in 2 consecutive bytes) which represents a trailer of a file.
    >>
    >> I can't work out how to do it.
    >>
    >> Any help would be appreciated.
    >>
    >> TIA
    >>

    >
    > assume byte[] b
    >
    > if (b[n] == (byte)0xff)
    > // true
    >
    > --
    >
    > Knute Johnson
    > email s/nospam/knute2008/
    >
    > --
    > Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    > ------->>>>>>http://www.NewsDemon.com<<<<<<------
    > Unlimited Access, Anonymous Accounts, Uncensored Broadband Access



    Thanks everybody, as usual you have come up trumps.

    Thanks from a Cobol programmer dabbling in Java!!

    --
    Steve
     
    Steve Rainbird, Jul 17, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Terry Reedy
    Replies:
    0
    Views:
    90
    Terry Reedy
    Nov 18, 2013
  2. Fabio Zadrozny
    Replies:
    0
    Views:
    116
    Fabio Zadrozny
    Nov 18, 2013
  3. Colin J. Williams
    Replies:
    0
    Views:
    115
    Colin J. Williams
    Nov 18, 2013
  4. Tim Golden
    Replies:
    0
    Views:
    101
    Tim Golden
    Nov 27, 2013
  5. Terry Reedy
    Replies:
    0
    Views:
    78
    Terry Reedy
    Nov 27, 2013
Loading...

Share This Page