'javabyte'에 해당되는 글 1건

  1. 2020.11.11 int를 byte배열로 반환
Java2020. 11. 11. 14:59

 

 

 

int형의 값을 byte array로 변환할때 찾아서 썼던 메소드이다.

ByteBuffer를 이용한 방법이 편리했었던 기억이 난다.

 

 

/**
	 * int를 byte배열로 변경하는 함수
	 * 
	 * @param date
	 * @return byte[]
	 */
	public static byte[] intToByte(int num) {
		ByteBuffer buff = ByteBuffer.allocate(Integer.SIZE / 8);
		ByteOrder order = ByteOrder.LITTLE_ENDIAN;
		buff.order(order);

		// 인수로 넘어온 integer을 putInt로설정
		buff.putInt(num);
		return buff.array();
	}

 

 

'Java' 카테고리의 다른 글

Java 오늘 날짜 구하기  (0) 2020.11.11
byte 배열을 short으로  (0) 2020.11.11
CPU, 메모리 사용량 반환  (0) 2020.11.11
이클립스 switch문을 if문으로  (0) 2020.11.11
바이트배열을 헥사값으로 변환  (0) 2020.11.11
Posted by 사슴영혼'-'