java如何把int,按4位一取,转换成16进制输出? 问题还可以变一下:byte[] b={12,14,15,16};把b连成一个32位的;然后按4位一取,换成16进制,输出? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class ByteTran { static byte[] b={12,14,15,16}; public static void main(String[] args){ Integer resut = 0; for(int i = 0; i < b.length; i++){ resut |= (b[i]<<((b.length-i-1)*8)); } //输出十六进制 System.out.println(Integer.toHexString(resut)); }} 一般int变成byte数组是反的。高位在后,低位在前。byte[] b={12,14,15,16};int convertBytesToInt(byte[] b){ return (b[3]<<24)|(b[2]<<16)|(b[1]<<8)|b[0];} 补充一下,由于java里面没有unsigned byte,所以需要通过&0xff将符号位去掉。int convertBytesToInt(byte[] b){if(b == null || b.length != 4) return 0; int n0 = b[3]; n0= (n0&0xff)<<24; int n1 = b[2]; n1= (n1&0xff)<<16; int n2 = b[1]; n2= (n2&0xff)<<8; int n3 = b[0]; n3= (n3&0xff)<<24; return n0|n1|n2|n3;} int convertBytesToInt(byte[] b){if(b == null || b.length != 4) return 0; int n0 = (b[3]&0xff)<<24; int n1 = (b[2]&0xff)<<16; int n2 = (b[1]&0xff)<<8; int n3 = (b[0]&0xff); return n0|n1|n2|n3;} Activity跳转以及Handler问题 如何引用第三方SO文件 android开发调用Intent出错 widget添加至桌面代码怎么实现 如何完全退出程序 小妹继续提问在Notification显示下载列表问题。 求助高手。怎样可以自定义android的键值 想入门android,大家能不能推荐基本好书 andirod 访问webservice 融云eclipse集成 demo项目出现的问题,求大神指导 一个图片切换效果内存溢出的问题 getHeight()方法返回的高度超出屏幕!!!!
static byte[] b={12,14,15,16};
public static void main(String[] args){
Integer resut = 0;
for(int i = 0; i < b.length; i++){
resut |= (b[i]<<((b.length-i-1)*8));
}
//输出十六进制
System.out.println(Integer.toHexString(resut));
}
}
int convertBytesToInt(byte[] b)
{
return (b[3]<<24)|(b[2]<<16)|(b[1]<<8)|b[0];
}
{
if(b == null || b.length != 4)
return 0; int n0 = b[3];
n0= (n0&0xff)<<24;
int n1 = b[2];
n1= (n1&0xff)<<16;
int n2 = b[1];
n2= (n2&0xff)<<8;
int n3 = b[0];
n3= (n3&0xff)<<24;
return n0|n1|n2|n3;
}
{
if(b == null || b.length != 4)
return 0; int n0 = (b[3]&0xff)<<24;
int n1 = (b[2]&0xff)<<16;
int n2 = (b[1]&0xff)<<8;
int n3 = (b[0]&0xff);
return n0|n1|n2|n3;
}