将int转换为unsigned short java [英] Convert int to unsigned short java
问题描述
我在java中编写了一个.obj解析器来模拟iPhone上的3D对象。我想将数据导出为二进制文件,该文件必须尽可能小。我有很多适合无符号短的索引,但它们在java中表示为int。
我想在文件中写入数据之前使用ByteBuffer类进行转换。我想在将它们推入ByteBuffer之前我必须先操作字节,但我不知道怎么做。
如果你能帮助我,请提前感谢你。<在p中,unsigned short可以表示为 char
。只需将int转换为char并在ByteBuffer上使用 putChar()
。
myBuffer.putChar((char)my16BitInt);
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.
In Java, an unsigned short can be represented as a char
. Just cast the int to char and use putChar()
on the ByteBuffer.
myBuffer.putChar((char) my16BitInt);
这篇关于将int转换为unsigned short java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!