我试着写TIFF IFDs,并且我在寻找一个简单的方法来做到以下(这个代码显然是错误的,但它得到的想法越过我想要什么):

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)

将编写:

0c00 0301 0300 0100 0000 0100 0000

我知道如何获得书面方法来采取了正确的数字节(writeInt,writeChar,等等),但我不知道怎么把它写在little endian.任何人,知道吗?

有帮助吗?

解决方案

也许你应该尝试是这样的:

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();     

其他提示

的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);
}

检查了 ByteBuffer, 具体''方法。ByteBuffer是一个祝福我们这些人需要接口,与任何不Java。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top