rt

解决方案 »

  1.   

    我写的代码。不过现在还有问题,高手指点long p = new Random().nextLong();
            System.out.println(p);
            byte[] a = new byte[8];
            for(int i=0;i<8;i++)
            {
             a[i] = (byte) (p%(256));
             p = p>>>8;
            }
            System.out.println(new String(a));
            long pp=0;
            
            for(int i=7;i>=0;i--)
            {
             pp = pp+a[i];
             pp = pp<<8;
            }
    System.out.println(pp);
    System.out.println(pp==p);
      

  2.   

    有没有API啊,我没有找到,高手指点啊
    上面的比较的时候有问题,这会改好的了
    long p = new Random().nextLong();
    long p2= p;
            System.out.println(p);
            byte[] a = new byte[8];
            for(int i=0;i<8;i++)
            {
             a[i] = (byte) (p%(256));
             p = p>>>8;
            }
            
            
            //System.out.println(new String(a));
            long pp=0;
            
            for(int i=7;i>=0;i--)
            {
             pp = pp+a[i];
             pp = pp<<8;
            }
    System.out.println(pp);
    System.out.println(pp==p2);
      

  3.   

    public static long readLong(byte[] b, int index) {
            long r = 0;
            for (int i = index; i < b.length; i++) {
                r <<= 8;
                r |= (b[i] & 0x00000000000000ff);
            }
            return r;
        }
         
        public static byte[] longToBytes(long n) {
            byte[] buf = new byte[8];
            for (int i = buf.length - 1; i >= 0; i--) {
                buf[i] = (byte) (n & 0x00000000000000ff);
                n >>= 8;
            }
            return buf;
        }
      

  4.   

    Byte.parseByte(String);Byte.toString(byte b)=string;
    Long.parseLong(String);Long.toString(long i)=string;
      

  5.   

    waterborn(WaterBorn)有点问题哦:
    public static long readLong(byte[] b, int index) {
            long r = 0;
            for (int i = index; i < b.length; i++) {
                r <<= 8;
                r += (b[i] & 0x00000000000000ff);//累加
            }
            return r;
        }
         
        public static byte[] longToBytes(long n) {
            byte[] buf = new byte[8];
            for (int i = buf.length - 1; i >= 0; i--) {
                buf[i] = (byte) (n & 0x00000000000000ff);
                n >>>= 8;//无符号左移会不会保险些
            }
            return buf;
        }
      

  6.   

    pp = pp+a[i];
    pp = pp<<8;
    应该先移位后加余数;
    但是你没有考虑到byte里的符号位。比如11000000无符号是192,byte中是-64
      

  7.   

    首先说twowolf(两只狼) 用API可以解决问题, 不过现在要是自己用代码来写就发现很多问题,我那个肯定写错了,应该用&和|来解决这个问题