무엇이 패킷 데이터 앞에 플러스 패킷 헤더의 6 바이트를 구성하는

여기입니다 바오 터우의 정의입니다

패킷 길이 (2 바이트), 데이터 타입 (2 바이트), 데이터 길이 (2 바이트) ............. 데이터 (가변 길이)

/**
	 * 将原数据包转为带6个字节包头的数据包
	 * @param type (十进制)数据类型
	 * @param message (不带包头)数据包
	 * @author xxs
	 * @return
	 */
	public static byte[] getSendContent(int type, byte[] message) {
		  int messageLength = message.length;
		  int contentLength = message.length + 6;
		  
		  
		     
		  byte message_low = (byte) (messageLength & 0x00ff); // 获得信息低位字节
		  byte message_high = (byte) ((messageLength >> 8)&0xff);// 获得信息高位字节

		  byte type_low = (byte) (type & 0x00ff); // 获得类型低位字节
		  byte type_high = (byte) ((type >> 8)&0xff);// 获得类型高位字节

		  byte content_low = (byte) (contentLength & 0x00ff); // 获得内容长度低位字节
		  byte content_high = (byte) ((contentLength >> 8)&0xff);// 获得内容长度高位字节

		  byte[] headMessage = new byte[6];
		  headMessage[0] = content_low;
		  headMessage[1] = content_high;
		  headMessage[2] = type_low;
		  headMessage[3] = type_high;
		  headMessage[4] = message_low;
		  headMessage[5] = message_high;

		  byte[] sendContent = new byte[contentLength];
		  System.arraycopy(headMessage, 0, sendContent, 0, 6);
		  System.arraycopy(message, 0, sendContent, 6, messageLength);
		  return sendContent;
		 }

또한 System.arraycopy에 () 메소드는 소스 보았다. 메인 어레이 카피 다른 배열을 따르고있다.

공공 정적 무효 arraycopy (객체 SRC, INT로부터 srcPos 개체 최종 도착, INT destPos가, INT 길이)
코드 명 :
  Object 주위 SRC : 소스 어레이
   의 INT로부터 srcPos : 메타 데이터의 시작 위치에서
  타겟 어레이 : 개체 최종 도착
  INT의 destPos가 : 대상 배열 시작 위치를 시작
  INT 길이 : 복사 어레이의 길이

예를 들면 :

우리는 데이터 바이트의 배열이 [] srcBytes 새로운 바이트 = [{2,4,0,0,0,0,0,10,15,50는} // 광원 어레이

                                    바이트 [] = destBytes 새로운 바이트 [5]; // 대상 배열

우리는 변환에 System.arraycopy에를 사용 (복사)

System.arraycopy에 (srcBytes, 0, destBytes, 0,5)
상기 코드 :

빈 1 차원 배열의 배열 (12)의 총 길이는, 상기 타겟 어레이 0~5 사이 destBytes의 광원 어레이 srcBytes 값의 복사본을 만들고, 상기 타겟 어레이의 비트 0에서 시작.
그런 이 코드 줄은 2,4,0,0,0해야 운영 결과,

 

 

 

게시 된 141 개 원래 기사 · 원 찬양 33 ·은 50000 +를 볼

추천

출처blog.csdn.net/qq_43560721/article/details/102831777