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;
}
解决方案 »
- 关于线程中断的疑问。
- 4个问题
- 修改excel文件的数据
- 怎么样把java类加载到数据库中?
- 菜鸟的简单问题!!!!~~~~~~~
- 如何打印在Frame内的JEditorPane的内容? 是的,只打JEditorPane的内容.
- 有谁知道Jbuilder的中文版教程可以下载站点,希望能让所有java爱好者分享,壮大我们java程序员的力量,振兴国内的java事业!
- 急救!!!!一个大小写的问题!给100
- 怎么做一个精确的计时器
- 如果选择一个偶数会在乘法运算中产生溢出,导致数值信息丢失,因为乘二相当于移位运算
- 如何读取空字段不出错
- 有哪个大哥大姐知道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