Created
August 2, 2012 14:12
-
-
Save nathanchen/3237363 to your computer and use it in GitHub Desktop.
JAVA - 比较两个字节数组
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public int compareTo(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2) | |
| { | |
| if(buffer1 == buffer2 && offset1 == offset2 && length1 == length2) | |
| return 0; | |
| int end1 = offset1 + length1; | |
| int end2 = offset2 + length2; | |
| for(int i = offset1, j = offset2; i < end1 && j < end2; | |
| i++, j++) | |
| { | |
| int a = (buffer1[i] & 0xff); | |
| int b = (buffer2[j] & 0xff); | |
| if(a != b) | |
| return a - b; | |
| } | |
| return length1 - length2; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment