2009-09-08 3 views
16

TIFF IFD를 쓰려고하는데, 다음과 같은 간단한 방법을 찾고 있습니다. (이 코드는 분명히 잘못되었지만 원하는 것부터 아이디어를 얻습니다) :(java) 파일 리틀 엔디안 작성하기

0C00 0301 0300 0100 0000 0100 0000

내가 바이트 (내의 writeInt,는 writeChar 등)의 정확한 수를 차지하기 위해 쓰는 방법을 얻는 방법을 알고 있지만 :

out.writeChar(12) (bytes 0-1) 
out.writeChar(259) (bytes 2-3) 
out.writeChar(3) (bytes 4-5) 
out.writeInt(1) (bytes 6-9) 
out.writeInt(1) (bytes 10-13) 

쓸 것인가 리틀 엔디안으로 쓰는 방법을 모르겠습니다. 누구 알아?

답변

31

어쩌면 당신은 이런 식으로 뭔가를 시도해야합니다 :

ByteBuffer buffer = ByteBuffer.allocate(1000); 
buffer.order(ByteOrder.LITTLE_ENDIAN);   
buffer.putChar((char) 12);      
buffer.putChar((char) 259);      
buffer.putChar((char) 3);      
buffer.putInt(1);        
buffer.putInt(1);        
byte[] bytes = buffer.array();  
+0

+1 코드 샘플 – dhable

6

ByteBuffer, 특히 'order'방법을 확인하십시오. ByteBuffer는 Java가 아닌 다른 것과 인터페이스해야하는 우리에게 축복입니다.

7

의 ByteBuffer 분명히 더 나은 선택입니다. 다음과 같은 편리한 함수를 작성할 수도 있습니다.

public static void writeShortLE(DataOutputStream out, short value) { 
    out.writeByte(value & 0xFF); 
    out.writeByte((value >> 8) & 0xFF); 
} 

public static void writeIntLE(DataOutputStream out, int value) { 
    out.writeByte(value & 0xFF); 
    out.writeByte((value >> 8) & 0xFF); 
    out.writeByte((value >> 16) & 0xFF); 
    out.writeByte((value >> 24) & 0xFF); 
} 
관련 문제