由4个字节数组转换为32位有符号整数-java语言实现

wangzhjsz 2012-03-31 02:28:09
情况是这样的,有个字节数组如:
Byte tmpByte[]={88,129,89,1};
现要将这个字节tmpByte数组的4个字节转换为32位的有符号的整型,
结果是:22643032
请问,这个是怎么转过来的 ,java里面有没有现成的函数可用呢
...全文
1300 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiakai0419 2012-10-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

低位前置结构
{88,129,89,1} =>
十六进制{01,59,89,58} =>
0x01598958 =>
22643032

tmpByte[3] << 24 + tmpByte[2] << 16 + tmpByte[1] << 8 + tmpByte[0]
[/Quote]

叫小端模式更合适吧。
铁猴 2012-10-10
  • 打赏
  • 举报
回复
为什么要右移八位以及&0xff了?
wangzhjsz 2012-03-31
  • 打赏
  • 举报
回复
把上面2段重新整理下:

byte[] byteArray = { 0x00, 0x00, 0x0e, 0x89, 0x00, 0x00, 0x27, 0xa7 };
// write bigid
byte[] intByte = System.BitConverter.GetBytes(bigid);
byteArray[0] = intByte[3];
byteArray[1] = intByte[2];
byteArray[2] = intByte[1];
byteArray[3] = intByte[0];
// write smid
intByte = System.BitConverter.GetBytes(smid);
byteArray[4] = intByte[3];
byteArray[5] = intByte[2];
byteArray[6] = intByte[1];
byteArray[7] = intByte[0];
/////////////////////////////////////////////////
Stream dataStream =...;
BinaryReader BR = new BinaryReader(dataStream);
// skip 3 byte
BR.ReadByte();
BR.ReadByte();
BR.ReadByte();
if (0 == BR.ReadInt32())
{
byte[] tmpByte = new byte[4];
tmpByte[3] = BR.ReadByte();
tmpByte[2] = BR.ReadByte();
tmpByte[1] = BR.ReadByte();
tmpByte[0] = BR.ReadByte();
int tiia = BitConverter.ToInt32(tmpByte, 0);
out double tA = (double)tiia / 1000000D;
}


wangzhjsz 2012-03-31
  • 打赏
  • 举报
回复
下面这段也是aspx代码:如何转换为java代码
Stream dataStream ;
BinaryReader BR = new BinaryReader(dataStream);
// skip 3 byte
BR.ReadByte();
BR.ReadByte();
BR.ReadByte();
if (0 == BR.ReadInt32())
{
byte[] tmpByte = new byte[4];
tmpByte[3] = BR.ReadByte();
tmpByte[2] = BR.ReadByte();
tmpByte[1] = BR.ReadByte();
tmpByte[0] = BR.ReadByte();
int tiia = BitConverter.ToInt32(tmpByte, 0);
out double tA = (double)tiia / 1000000D;
}
wangzhjsz 2012-03-31
  • 打赏
  • 举报
回复
下面是 aspx里面的 一段代码:如何用java来改写?
byte[] byteArray = { 0x00, 0x00, 0x0e, 0x89, 0x00, 0x00, 0x27, 0xa7 };
// write bigid
byte[] intByte = System.BitConverter.GetBytes(bigid);
byteArray[48] = intByte[3];
byteArray[49] = intByte[2];
byteArray[50] = intByte[1];
byteArray[51] = intByte[0];
// write smid
intByte = System.BitConverter.GetBytes(smid);
byteArray[52] = intByte[3];
byteArray[53] = intByte[2];
byteArray[54] = intByte[1];
byteArray[55] = intByte[0];
calm677 2012-03-31
  • 打赏
  • 举报
回复

int s=0;
for(int i=0;i<tmpByte.length;i++){
s<<=8;
s|=(tmpByte[i] & 0x000000ff);
}
return s;

beiouwolf 2012-03-31
  • 打赏
  • 举报
回复
低位前置结构
{88,129,89,1} =>
十六进制{01,59,89,58} =>
0x01598958 =>
22643032

tmpByte[3] << 24 + tmpByte[2] << 16 + tmpByte[1] << 8 + tmpByte[0]
zqfddqr 2012-03-31
  • 打赏
  • 举报
回复
自己转其实很快的
luffy1010 2012-03-31
  • 打赏
  • 举报
回复
强制转换
wangzhjsz 2012-03-31
  • 打赏
  • 举报
回复
byte[] tmpByte = {88,129,89,1};
上面搞错了, 纠正;是这样的

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧