站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题如何把把一个int数放到一个BYTE[4]的数组里去?

如何把把一个int数放到一个BYTE[4]的数组里去?

摘要: 如何把把一个int数放到一个BYTE[4]的数组里去?


我新手,求教阿,帮我想想算法阿


for (int i = 0 ; i -#60; 4 ; i++)
byArray[i]=(BYTE)(iNum-#62;-#62;(i-#60;-#60;3));

是不是这样?


那如果要还原怎么还原阿


public class test {

public static void main(String[] args) {
int iNum = 65534;
byte[] byArray = new byte[4];
for (int i = 0; i -#60; 4; i++) {
byArray[i] = (byte) (iNum -#62;-#62; (i -#60;-#60; 3));
System.out.println(byArray[i]);
}
}
}
结果是:
-2
-1
0
0
不太对阿



public class IntUtil {
public static void print(byte b){
for (int i=0;i-#60;8;i++){
if ((b-#38;1-#60;-#60;(7-i))==0){
System.out.print(-#34;0-#34;);
}else{
System.out.print(-#34;1-#34;);
}
}
System.out.print(-#34; -#34;);
}
public static byte[] intToByteArray(int src){
byte[] b=new byte[4];
for (int i=0;i-#60;4;i++){
b[i]=(byte)(src-#62;-#62;-#62;(8*(3-i)));
print(b[i]);
}
return b;
}

public static int ByteArrayToInt(byte[] src){
int b=0;
for (int i=0;i-#60;4;i++){
for (int j=0;j-#60;8;j++){
if ((src[i]-#38;1-#60;-#60;(7-j))==0){
b-#38;=~(1-#60;-#60;((3-i)*8+(7-j)));
}else{
b|=(1-#60;-#60;((3-i)*8+(7-j)));
}
}
}
return b;
}

public IntUtil() {
}

public static void main(String[] args) {
for (int i=-1;i-#62;-1000;i--){
System.out.println(ByteArrayToInt(intToByteArray(i)));
}
}
}


↑返回目录
前一篇: 读取文件时在jbuilder中出错,但在jcreator中没有问题
后一篇: 如何删除properties文件的属性