Compare two Byte Arrays? (Java)

前端 未结 6 1225
天涯浪人
天涯浪人 2020-12-08 01:46

I have a byte array with a ~known binary sequence in it. I need to confirm that the binary sequence is what it\'s supposed to be. I have tried .equals in additi

6条回答
  •  失恋的感觉
    2020-12-08 02:12

    Of course, the accepted answer of Arrays.equal( byte[] first, byte[] second ) is correct. I like to work at a lower level, but I was unable to find a low level efficient function to perform equality test ranges. I had to whip up my own, if anyone needs it:

    public static boolean ArraysAreEquals(
     byte[] first,
     int firstOffset,
     int firstLength,
     byte[] second,
     int secondOffset,
     int secondLength
    ) {
        if( firstLength != secondLength ) {
            return false;
        }
    
        for( int index = 0; index < firstLength; ++index ) {
            if( first[firstOffset+index] != second[secondOffset+index]) {
                return false;
            }
        }
    
        return true;
    }
    

提交回复
热议问题