int a;
byte buffer[]=byte[4];
JAVA好像不支持这种直接的操作!byte[] 转换成 int
for (int i=0;i<buffer.length;i++)
a += ((int) buffer[i])*256;int 转换成 byte
int i=0;
while (a>0)
{
buffer[i++] = a % 256;
a = a % 256;
}
byte buffer[]=byte[4];
JAVA好像不支持这种直接的操作!byte[] 转换成 int
for (int i=0;i<buffer.length;i++)
a += ((int) buffer[i])*256;int 转换成 byte
int i=0;
while (a>0)
{
buffer[i++] = a % 256;
a = a % 256;
}
解决方案 »
- 弱问 请问Sun公司有没有提供实现类库的源代码,比如想Console类的源代码
- 老问题: Java 回车(\n)和换行(\r)的区别
- setText后初始值为什么没变?
- 用动态代码来批量验证Http代理问题[有点难度,感兴趣的进来看看吧,100分]
- 【救急!】JTree 如何删除指定字符串的一个节点
- [求助]UnsupportedServiceException出问题的原因是什么?
- 谈谈Pet Store(宠物店)例子的学习
- 大家帮我建立一个java环境吧。分全给你了。
- 从JLabe中将一张图片拖出来,放到一个Panel中,请问这能实现吗?
- 用java能否开发图形报表程序
- 如何读取空字段不出错
- 有哪个大哥大姐知道java中StringReader()的用法与意义。
byte有个包装类Byte。里面有个专门转换成int的方法(intValue()),返回的就是int型的值。
int有个包装类Interger。里面也有个方法(byteValue() ),返回的是byte型的值。
这样就可以自如的转换了。至于他们的数组间的转换,就是用循环完成就可以了嘛!
{
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;
}
byte可是有符号的
public static int[] _MoveX={0xff,0xff00,0x0ff0000,0xff000000};
public static byte[] uintoBytes(int IntegerSize){ byte Byte4Size[] = new byte[4]; for(int i = 0 ;i<4;i++) Byte4Size[i] = (byte)((IntegerSize & _MoveX[i]) >> (i*8)); return Byte4Size;}
public static int byte4toInt(byte[] Byte4Size){ int integer = 0; for(int i = 0;i<4;i++){ int int_temp = 0; int_temp= int_temp | ((int)Byte4Size[i] & 0xff); integer = integer | (int_temp<<(i*8)); } return integer;}
Exeception in thread "main" java.lang.NoSuchMethodError:mainint bytes;
byte signBuffer[]=new byte[4];int to byte[] : signBuffer=((new Integer(bytes)).toString()).getBytes();byte[] to int: bytes=new Integer(new String(signBuffer)).intValue();
有眉头正确设置
classpath