如何将4字节数组转换为整数? [英] How can I convert a 4-byte array to an integer?

查看:932
本文介绍了如何将4字节数组转换为整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在不诉诸某些依赖于实现的技巧的情况下执行转换。有小费吗?

I want to perform a conversion without resorting to some implementation-dependent trick. Any tips?

推荐答案

您需要知道字节的字节顺序。

You need to know the endianness of your bytes.

假设(如@ WhiteFang34) bytes 是长度为4的 byte [] ,那么......

Assuming (like @WhiteFang34) that bytes is a byte[] of length 4, then...

Big-endian:

Big-endian:

int x = java.nio.ByteBuffer.wrap(bytes).getInt();

Little-endian:

Little-endian:

int x = java.nio.ByteBuffer.wrap(bytes).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();

这篇关于如何将4字节数组转换为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆