views:

603

answers:

3

hi,i have byte array stored by both hexadecimal and decimal value,i want to search for hexadecimal 1 i'e SOH in the how can i do this in java,plz give a sample code. int SOH=0X01; if(SOH==1)

is showing true.is it correct or not.

thaks in advance.

+1  A: 

How is the data in the byte array stored as hexadecimal and decimal? A byte array contains bytes.

     byte[] decimal = new byte[] {1,10 };
     byte[] hexa = new byte[] {0x1,0xa };

These contain the same values, you can compare them directly, you don't need any specific code.

Nicolas
+3  A: 

Your byte arrays will just store byte values. The hexadecimal (or decimal, or octal) is just the representation of that value in the source code. Once stored, they're all the same value e.g.

0x01 == 1 == 01

(the last being octal)

So checking for a particular value is the same code. A value won't know if it's been represented as hex/dec/oct.

Brian Agnew
A: 

not sure if that really solves your problem

private static final byte SOH = 1;  // or 0x01 if you prefer, is the same value

byte[] data = ...

for (int i = 0; i < data.length; i++) {
    if (data[i] == SOH) {
        // do what you need
        break;   // don't if you want to find all SOH
    }
}
Carlos Heuberger