Check for 0xff in byte array

S

Steve Rainbird

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
 
K

Knute Johnson

Steve said:
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
 
M

Mark Space

Knute said:
Steve said:
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.
 
M

Mark Space

Peter said:
[...]
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.
 
J

Joshua Cranmer

Mark said:
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.
 
M

Mark Space

Joshua said:
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.
 
S

Steve Rainbird

Knute Johnson said:
Steve said:
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


Thanks everybody, as usual you have come up trumps.

Thanks from a Cobol programmer dabbling in Java!!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top