little-endian java,(java)在文件little endian中编写

博客内容涉及如何使用Java的ByteBuffer类以小端序(little-endian)方式写入字符和整数到字节流中。作者展示了如何分配ByteBuffer,设置字节顺序,并通过putChar和putInt方法写入数据,最后将缓冲区内容转换为字节数组。
摘要由CSDN通过智能技术生成

I'm trying to write TIFF IFDs, and I'm looking for a simple way to do the following (this code obviously is wrong but it gets the idea across of what I want):

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)

Would write:

0c00 0301 0300 0100 0000 0100 0000

I know how to get the writing method to take up the correct number of bytes (writeInt, writeChar, etc) but I don't know how to get it to write in little endian. Anyone know?

解决方案

Maybe you should try something like this:

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值