int a=278;
byte[] b=new byte[4];
b=(Integer.toBinaryString(a)).getBytes();
byte[] b=new byte[4];
b=(Integer.toBinaryString(a)).getBytes();
解决方案 »
- 求助
- ireport针打不正常及如何精确控制纸长?
- 关于日期比较的问题 java实现
- 十进制的数转换为十六进制
- 帧回调函数 java
- 有一道JAVA题,请教各位论坛上高手~~
- 怎样使生成的窗口不会被任意拉大?
- 那里有Borland Opimizeitsuite 5.5或者JProbe下载?
- 请问高手们,怎样把int 转换成String 型
- 我的性能图怎么会出这种问题呢!!!!!到底怎么回事啊??我好伤心的,GG救救我拉!那个JScrollPane到底有什么玄机啊~~~~~~~~~~~~~~~~~~~~
- 在问关于java连接数据库的问题。
- jdk1.4在哪下载?JAVA.SUM.COM上面都是英文我看不懂下载哪个?
返回的b变成了byte[9],超过了四个字节,这是怎么回事?
方法一:
1:String str = Integer.toBinaryString(a)得到一个由1,0组成的字符串
2:if (str.length() < 32) 在str前面补(32-str.length())个0,得到长度是32的由1,0组成的字符串
3:每8位一组,substring成4段
4:每段转化成一个整数放到byte[]里面。
方法二:
就是把整数手工转化成256进制的数,然后放入数组,数组每个元素保存一位。
int n = 278;
byte[] b = new byte[4];
for (int i = 0; i < 4; i++) {
b[i] = (byte) (n % 256);
n /= 256;
}
int value = ...
OutputStream out = ...
DataOutputStream dos = new DataOutputStream(out);
dos.writeInt(value);InputStream in = ...
DataInputStream dis = new DataInputStream(in);
int value = dis.readInt();
{
int temp = number;
byte[] b=new byte[4];
for (int i = b.length - 1; i > -1; i--)
{
b[i] = new Integer(temp & 0xff).byteValue();
temp = temp >> 8;
}
return b;
} public static int toInteger(byte[] b)
{
int s = 0;
for (int i = 0; i < 3; i++)
{
if (b[i] >= 0)
s = s + b[i];
else
s = s + 256 + b[i];
s = s * 256;
}
if (b[3] >= 0)
s = s + b[3];
else
s = s + 256 + b[3];
return s;
}// 字符到字节转换
public static byte[] CharToByte(char ch){
int temp=(int)ch;
byte[] b=new byte[2];
for (int i=b.length-1;i>-1;i--){
b[i] = new Integer(temp&0xff).byteValue(); //将最高位保存在最低位
temp = temp >> 8; //向右移8位
}
return b;
}// 字节到字符转换
public static char ByteToChar(byte[] b){
int s=0;
if(b[0]>=0)
s+=b[0];
else
s+=256+b[0];
s*=256;
if(b[1]>=0)
s+=b[1];
else
s+=256+b[1];
char ch=(char)s;
return ch;
}
}