'Integer2ByteArray'에 해당되는 글 1건

  1. 2014.05.30 Byte Array <-> Integer

integer 를 byte array 로 바꾸는 방법

ByteBuffer 클래스를 사용하면 간단하게 바꿀 수 있다.




	private int byteArray2int(byte[] data)
	{
		ByteBuffer buffer = ByteBuffer.allocate(Integer.SIZE/8);				//integer(4byte) 크기로 buffer 생성
		buffer.order(ByteOrder.BIG_ENDIAN);							//big endian 방식으로 정렬  예) 1025 = 0x00 0x00 0x04 0x01
		buffer.put(data);											//buffer에 byte array 데이터를 넣어준다.
		buffer.flip();											//put을 해주면 buffer의 position이 마지막 위치로 이동하는데 이것을 다시 0으로 되돌려준다.
		
		return buffer.getInt();										//position 위치로부터 integer 값을 가져온다.
	}



	private byte[] int2byteArray(int data)
	{
		ByteBuffer buffer = ByteBuffer.allocate(Integer.SIZE/8);				//integer(4byte) 크기로 buffer 생성
		buffer.order(ByteOrder.BIG_ENDIAN);							//big endian 방식으로 정렬  예) 1025 = 0x00 0x00 0x04 0x01
		buffer.putInt(data);										//buffer에 integer 데이터를 넣어준다.
		return buffer.array();										//byte array 를 가져온다.
	}


'Development > Java' 카테고리의 다른 글

구글 플레이 게임 서비스 자동 로그인 설정 / 해제하기  (0) 2014.06.23
javadoc 작성법  (0) 2013.10.22
제곱 구하는 방법  (0) 2013.09.04
각도 구하는 방법  (0) 2013.09.04
Posted by 후미카
:

카테고리

전체보기 (102)
Development (102)

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

달력

«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

글 보관함