没有必要这样复杂。
Socket s;
DataInputStream io_in;
int i;
.....io_in=s.getDataInputStream();
...
i=in_in.readInt();
Socket s;
DataInputStream io_in;
int i;
.....io_in=s.getDataInputStream();
...
i=in_in.readInt();
调试欢乐多
byte[] a={'0','0','0','1'};
怎样转换成整数
在c里面可以
BYTE a[]={'0','0','0','1'};
int *x;
x=(int *)a;
因为int是4个字节,所以刚好把a全部包括进去了
所以这时候x的值是16进制的0X31303030,低字节在前,高字节在后
你的问题是2byte->1int,网络顺序是高位在前低位在后.你要手工转,我就是用了苯办法,用了个switch语句,不过有更好的方法,用移位更好,我没有时间来改写,你也可以手工转一下.
Translates a byte array containing the two's-complement binary representation of a BigInteger into a BigInteger.
BigInteger(int signum, byte[] magnitude)
Translates the sign-magnitude representation of a BigInteger into a BigInteger.
BigInteger bi = new BigInteger(a);
int i = bi.intValue();
public static int byteToint(byte[] convertByteValue)
{ byte[] YY=new byte[4];
YY=convertByteValue;
int ee, ff, gg, hh;
ee = YY[3] & 0x000000ff;
//System.out.println("ee: " +ee);
ff = (YY[2]<<8) & 0x0000ff00;
//System.out.println("ff: " +ff);
gg = (YY[1]<<16) & 0x00ff0000;
// System.out.println("gg: " +gg);
hh = YY[0]<<24;
// System.out.println("hh: "+hh);
int jj = ee + ff + gg + hh;
// System.out.println("jj: "+jj);
return jj;
} // Function :About the convert of int to byte array
public byte[] intTobyte(int convertIntValue)
{ int Y;
Y = 1321432453;
byte YY[] = new byte[4];
Integer aa = new Integer(Y);
YY[3] = aa.byteValue();
Integer bb = new Integer(Y>>>8);
YY[2] = bb.byteValue();
Integer cc = new Integer(Y>>>16);
YY[1] = cc.byteValue();
Integer dd = new Integer(Y>>>24);
YY[0] = dd.byteValue();
return YY;
}