将int转换为无符号短java
我用 java 编写了一个 .obj 解析器来对 iPhone 上的 3D 对象进行建模。我想将数据导出为二进制文件,该文件必须尽可能小。我有很多适合无符号短整型的索引,但它们在 java 中表示为 int 。
我想在将数据写入文件之前使用 ByteBuffer 类进行转换。我想我必须在将字节推入 ByteBuffer 之前对其进行操作,但我不知道该怎么做。
如果您能帮助我,请提前致谢。
I have written a .obj parser in java to modelize 3D objects on iPhone. I would like to export the data as a binary file, which must be as small as possible. I have plenty of indices that would fit a unsigned short, but they are represented as int in java.
I would like to use the ByteBuffer class to do the conversion just before writing the data in a file. I suppose I will have to manipulate bytes before pushing them into the ByteBuffer but I have no idea how to do so.
Thank you in advance if you can help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Java 中,无符号短整型可以表示为
char
。只需将 int 转换为 char 并在 ByteBuffer 上使用putChar()
即可。In Java, an unsigned short can be represented as a
char
. Just cast the int to char and useputChar()
on the ByteBuffer.您可以使用以下命令从整数中提取单个字节
You can extract single bytes from your integers with
使用
http://download.oracle.com/javase/6 /docs/api/java/io/DataOutputStream.html
Use
http://download.oracle.com/javase/6/docs/api/java/io/DataOutputStream.html