Java中如何复制原始类型内存?
我有两个字符 = 4 个字节,代表整数值(从流中获取)。
如何将它们复制到原始 int 变量中?
I have two chars = 4 bytes, that representing integer value (geted from stream).
How can I copy these into a primitive int variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您最好从一开始就以
int
形式读取 4 个字节。但是,要将两个 char 转换为 int,您可以使用您需要知道顺序是小端还是大端。
You are better off reading 4 bytes as an
int
from the start. However to turn two char into an int you can useYou need to know whether the order is little or big endian.
这是一种可能的方法:
但是:您必须担心字节顺序以及
int
已签名的事实。Here's one possible way of doing it:
BUT: you'll have to worry about endianness, and about the fact that
int
is signed.