有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 bat运行java? 这是什么意思? 如何对list中每个对象修改? 救命!日文系统下 ‘~‘ 等符号绑到Combobox 变成‘?‘ 乱码,如何解决? A a = new B()与A b = new A()是一样的吗? 请问这到底是个什么错误啊?请指教 Socket Connection Timed Out 的问题 成员变量进行显示初始化,有什么好处? 这种情况下如何编译一个java class? 关于统计字符串的问题 =====请问哪里有这样的帮助文档下载?===== 最简单的问题
我要的是类似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