• c++和java字节高低位的转换


    现在我们需要从一个C/C++语言生成的二进制文件中读出一个float数据 

    Java代码 
    1. // 参见java.io.DataInputStream  
    2. // C++写入的字节顺序是从低到高(左低到右高),  
    3.    而java.io.DataInputStream读取的数据是从高到低(左高到右低)  
    4. // 所以需要自己改写一下  
    5. // 功能和java.io.DataInputStream类似的  
    6. public class CppInputStream extends FilterInputStream {  
    7.   
    8.   public CppInputStream(InputStream in) {  
    9.     super(in);  
    10.   }  
    11.   public final int read(byte b[]) throws IOException {  
    12.     return in.read(b, 0, b.length);  
    13.   }  
    14.   
    15.   public final int read(byte b[], int off, int len) throws IOException {  
    16.     return in.read(b, off, len);  
    17.   }  
    18.   
    19.   public final void readFully(byte b[]) throws IOException {  
    20.     readFully(b, 0, b.length);  
    21.   }  
    22.   
    23.   public final void readFully(byte b[], int off, int len) throws IOException {  
    24.     if (len < 0)  
    25.       throw new IndexOutOfBoundsException();  
    26.     int n = 0;  
    27.     while (n < len) {  
    28.       int count = in.read(b, off + n, len - n);  
    29.       if (count < 0)  
    30.         throw new EOFException();  
    31.       n += count;  
    32.     }  
    33.   }  
    34.   
    35.   public final int skipBytes(int n) throws IOException {  
    36.     int total = 0;  
    37.     int cur = 0;  
    38.     while ((total < n) && ((cur = (int) in.skip(n - total)) > 0)) {  
    39.       total += cur;  
    40.     }  
    41.     return total;  
    42.   }  
    43.   
    44.   public final byte readByte() throws IOException {  
    45.     int ch = in.read();  
    46.     if (ch < 0)  
    47.       throw new EOFException();  
    48.     return (byte) (ch);  
    49.   }  
    50.   
    51.   public final int readUnsignedByte() throws IOException {  
    52.     int ch = in.read();  
    53.     if (ch < 0)  
    54.       throw new EOFException();  
    55.     return ch;  
    56.   }  
    57.   
    58.   public final short readShort() throws IOException {  
    59.     int ch2 = in.read();  
    60.     int ch1 = in.read();  
    61.     if ((ch1 | ch2) < 0)  
    62.       throw new EOFException();  
    63.     return (short) ((ch1 << 8) + (ch2 << 0));  
    64.   }  
    65.   
    66.   public final int readUnsignedShort() throws IOException {  
    67.     int ch2 = in.read();  
    68.     int ch1 = in.read();  
    69.     if ((ch1 | ch2) < 0)  
    70.       throw new EOFException();  
    71.     return (ch1 << 8) + (ch2 << 0);  
    72.   }  
    73.   
    74.   public final char readChar() throws IOException {  
    75.     int ch2 = in.read();  
    76.     int ch1 = in.read();  
    77.     if ((ch1 | ch2) < 0)  
    78.       throw new EOFException();  
    79.     return (char) ((ch1 << 8) + (ch2 << 0));  
    80.   }  
    81.   
    82.   public final int readInt() throws IOException {  
    83.     int ch4 = in.read();  
    84.     int ch3 = in.read();  
    85.     int ch2 = in.read();  
    86.     int ch1 = in.read();  
    87.     if ((ch1 | ch2 | ch3 | ch4) < 0)  
    88.       throw new EOFException();  
    89.     return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));  
    90.   }  
    91.   
    92.   private byte readBuffer[] = new byte[8];  
    93.   
    94.   public final long readLong() throws IOException {  
    95.     readFully(readBuffer, 08);  
    96.     return (((long) readBuffer[7] << 56) + ((long) (readBuffer[6] & 255) << 48)  
    97.         + ((long) (readBuffer[5] & 255) << 40) + ((long) (readBuffer[4] & 255) << 32)  
    98.         + ((long) (readBuffer[3] & 255) << 24) + ((readBuffer[2] & 255) << 16)  
    99.         + ((readBuffer[1] & 255) << 8) + ((readBuffer[0] & 255) << 0));  
    100.   }  
    101.   
    102.   public final float readFloat() throws IOException {  
    103.     return Float.intBitsToFloat(readInt());  
    104.   }  
    105.   
    106.   public final double readDouble() throws IOException {  
    107.     return Double.longBitsToDouble(readLong());  
    108.   }  
    109. }  
    yy629 (资深程序员) 2010-05-12

    简单呀,用Java的字节流,读取一个int的4个字节,然后转换。 

    比如读取的数据是: 

    byte1 byte2 byte3 byte4 


    在其实这是C++的 byte4 byte3 byte2 byte1 

    那你可以用位运算转成Java中的对应的整数: 

    (byte1& 0xff)<<0  + (byte2& 0xff)<<8 + (byte3& 0xff)<< 16 +(byte4& 0xff)<<24 


    这样转换后的,就是Java中的整数了。 



    也可以先用Java读取一个Int进来,然后处理
     

    Java代码 
    1. // Java读取后,顺序已经反了  
    2. int javaReadInt = ;  
    3.   
    4. // 将每个字节取出来  
    5. byte byte4 = (byte) (javaReadInt & 0xff);  
    6. byte byte3 = (byte) ((javaReadInt & 0xff00) >> 8);  
    7. byte byte2 = (byte) ((javaReadInt & 0xff0000) >> 16);  
    8. byte byte1 = (byte) ((javaReadInt & 0xff000000) >> 24);  
    9.   
    10. // 拼装成 正确的int  
    11. int realint = (byte1& 0xff)<<0  + (byte2& 0xff)<<8 + (byte3& 0xff)<< 16 +(byte4& 0xff)<<24 ;  
    另外可以使用ByteBuffer来完成,而不需要自己考虑,如何将字节数组转换为其他数据类型. 使用ByteBuffer,可以设置字节顺序. 

    ByteBuffer简单的例子 

    import java.nio.ByteBuffer; 
    import java.nio.ByteOrder; 

    public class ByteBufferTest { 

      public static void main(String[] args) { 
        //将字节数组转换为int类型 
        byte[] bytes = {0,0,0,1}; 
        ByteBuffer buffer =  ByteBuffer.wrap(bytes); 
        System.out.println(buffer.getInt()); 
       
        ByteBuffer buffer2 = ByteBuffer.wrap(bytes); 
        buffer2.order(ByteOrder.LITTLE_ENDIAN); 
        System.out.println(buffer2.getInt()); 
        
      } 

    }
  • 相关阅读:
    J. 最大权边独立集 题解(树上背包)
    F. 地图压缩 题解(kmp 最小循环节)
    Sum of Log 题解(数位dp)
    F. Scalar Queries 题解(思维+线段树)
    B. 攻防演练 题解(思维+倍增)
    Bit Sequence 题解(数位dp)
    机器学习
    Android学习笔记
    sqoop
    Initialization failed for block pool Block pool
  • 原文地址:https://www.cnblogs.com/GameDeveloper/p/2099147.html
Copyright © 2020-2023  润新知