有byte[]与int转换的好方法吗? nBuf = Integer.parseInt(new String(syBuf));aybuf = Integer.toString(nbuf).getBytes(); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 2楼的答案风马牛不相及。我要的是类似C中的直接内存操作方式。你的答案类似于:nBuf=_atoi(ayBuf);sprintf("%04d",nBuf,ayBuf); JAVA里面没有相应的函数,自己写一个吧:public static int byteArr2Int(byte[] arrB){ if (arrB==null || arrB.length!=4) return 0; int i = (arrB[0]<<24) + (arrB[1]<<16) + (arrB[2]<<8) + arrB[3]; return i;}public static byte[] int2ByteArr(int i){ byte[] arrB = new byte[4]; arrB[0] = (byte)(i>>24); arrB[1] = (byte)(i>>16); arrB[2] = (byte)(i>>8); arrB[3] = (byte)i; return arrB;} 1>(int)ayBuf = nBuf.intValue()2>如 beming(Aming)所说的一样。 java没有指针的这种操作,会引起不安全因素,故此,要自己来写,参照ender(ender) 的方法了。 感谢ender。还有两点需要注意:1,int->bytes[]时,字节顺序要相反。2,byte<<24时,要判断byte是否为负,因为java没有unsigned 遇到了问题JAVA错误,帮忙看看! 大家帮我看下这几个选择题,在线等 文件中数据抽取 简单java网络程序(Socket-Tcp)遇到的奇怪问题 jexcelapi一个工作薄内复制问题 怎么用java压缩access数据库?? 帮帮我!关于网络编程!! 关于java的参数传递问题。 一个简单的小问题,各位高手帮帮忙! 为什么无法将从Properties中读出的数据转换为整型?在线等! =====请问哪里有这样的帮助文档下载?===== 最简单的问题
我要的是类似C中的直接内存操作方式。
你的答案类似于:
nBuf=_atoi(ayBuf);
sprintf("%04d",nBuf,ayBuf);
public static int byteArr2Int(byte[] arrB){
if (arrB==null || arrB.length!=4) return 0;
int i = (arrB[0]<<24) + (arrB[1]<<16) + (arrB[2]<<8) + arrB[3];
return i;
}public static byte[] int2ByteArr(int i){
byte[] arrB = new byte[4];
arrB[0] = (byte)(i>>24);
arrB[1] = (byte)(i>>16);
arrB[2] = (byte)(i>>8);
arrB[3] = (byte)i;
return arrB;
}
2>如 beming(Aming)所说的一样。
还有两点需要注意:
1,int->bytes[]时,字节顺序要相反。
2,byte<<24时,要判断byte是否为负,因为java没有unsigned