Java: determining how a byte array further comprises an array of bytes, the byte order and the same

aims

A byte array to implement a method further comprising a byte array B, the same sequence

principle

Setting a hit variable is initialized to 0, the number of bytes recorded two successive arrays of successive hits, the number of hits equal to the length of the byte array B, returns true, if recycled to the last byte or bytes shorter than the remaining bytes an array of length B, do not hit the variable B equal to the length of the byte array, false is returned

algorithm

  • 1. If the length is greater than B byte array byte array A direct return false
  • 2. Get cycle each byte value of the byte array
  • 3. Hit the variable B equal to the length of the byte array, returns true
  • 4. The current byte index value is equal to a byte value byte array B hits the variable values, and if so, increment the hit variable 1, continue to step 2 to continue to the next byte comparison value, otherwise continue with step 5
  • The variable is set hit 0
  • 6. A byte array determines the number of bytes of the remaining length is greater than the length of the byte array B, is larger than if the second execution jumps to step loop, or out of, returns false

practice

/**
	 * 判断是否一个字节数组按顺序包含另一个字节数组
	 * 
	 * @param pSrcByteArray
	 * @param pSubByteArray
	 * @return
	 */
	public static boolean isIncludeByteArray(byte[] pSrcByteArray, byte[] pSubByteArray) {
		boolean retValue = false;
		int lvSrcByteArrayLen = pSrcByteArray.length;
		int lvSubByteArrayLen = pSubByteArray.length;
		
		while(true) {
			if(lvSrcByteArrayLen < lvSubByteArrayLen) {
				break;
			}
			int lvHitByteNumberValue = 0;
			for(int i=0; i<lvSrcByteArrayLen; i++) {
				int tvByteValue = pSrcByteArray[i];
				if(lvHitByteNumberValue == pSubByteArray.length) {
					retValue = true;
					break;
				}
				if(tvByteValue == pSubByteArray[lvHitByteNumberValue]) {
					lvHitByteNumberValue++;
					continue;
				}
				lvHitByteNumberValue = 0;
				//剩余字节数
				int tvRemaindingByteLen = lvSrcByteArrayLen - i - 1;
				if(tvRemaindingByteLen < pSubByteArray.length) {
					break;
				}
			}
			break;
		}
		return retValue;
	}
Published 118 original articles · won praise 14 · views 50000 +

Guess you like

Origin blog.csdn.net/github_38641765/article/details/100123835