我正在尝试编写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等),但我不知道如何让它以小端字节序写入。有人知道吗?
发布于 2009-09-08 16:01:59
也许你应该试试这样的东西:
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(); 发布于 2009-09-08 16:05:34
显然,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);
}发布于 2009-09-08 15:53:40
查看ByteBuffer,特别是“order(http://java.sun.com/j2se/1.4.2/docs/api/java/nio/ByteBuffer.html#order(java.nio.ByteOrder%29)”方法。对于我们这些需要与任何东西交互的人来说,ByteBuffer是一种福气。
https://stackoverflow.com/questions/1394735
复制相似问题