利用String
public class test3{
   public static void main(String[] args){
    int    a = 1000;
    byte[]  b = new byte[4];
    int    c = 0;    
    String ss=Integer.toString(a);
    b=ss.getBytes();
    ss=new String(b);
    c=Integer.parseInt(ss);
    System.out.println(c);
  }
}

解决方案 »

  1.   

    版主理解有误吧。:) 按你的代码,int i = 10240; 的时候b就不是 byte[4]了。因为Java中int 就是4个byte,所以大概做法如下吧:for(int i=0; i<4; i++) {
       b[i] = (byte)( (a >>> i*8) & 0xFF );
    }c = 0;
    for(int i=3; i>=0; i--) {
       c |= b[i];
       c << 8;
    }上面的程序可能会有一大堆符号的问题吧,我也没空检查了,高手愿意就完善一下吧。
      

  2.   

    public class DateExa
    {
    public static void main(String[] args)
    {
    int a=1000;
         byte[] b=new byte[4];
         int  c = 0;
         for (int i=3;i>=0;i--)
         {
         int d=a%10;
         a=a/10;
         Integer Ia=new Integer(d);
         b[i]=Ia.byteValue();
         }
         for (int j=0;j<4;j++)
         {
         c=c*10;
         Byte Bb=new Byte(b[j]);
         c=c+Bb.intValue();
         }
         System.out.println(c);
         }
    }
    这样也可以
      

  3.   

    我同意luodi的观点.
    楼上兄弟的算法也是针对四位数的吧,对五位整数就不行啦!
      

  4.   

    我将luodi的程序改了一下,哈哈,借花献佛!
     
    public class Test
    {   
        public static void main(String[] args){
            int    a = -1000000000;
            byte[]  b = new byte[4];
            int    c = 0;    
            int    d;        for(int i=0; i<4; i++) {
                b[i] = (byte)( (a >>> i*8) & 0xFF );
            }
            for(int i=3; i>=0; i--) {
                d = 0x000000FF;
                d &= b[i];
                c |= d;
                if(i>0)
                    c <<= 8;            
            }
            System.out.println(c);
        }
    }