0 0

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

// 参见java.io.DataInputStream 
// C++写入的字节顺序是从低到高(左低到右高), 
   而java.io.DataInputStream读取的数据是从高到低(左高到右低) 
现在需要用java读取一个由c++生成的二进制文件中的int型数据、可是c++和java的字节高地位顺序不一样、需要转换有谁能知道怎么转换

2010年5月12日 11:14

5个答案 按时间排序 按投票排序

0 0

可以使用commons-io中的类SwappedDataInputStream
commons-io包可以到apache上下载

2010年5月13日 21:41
0 0

"yy629"回答的很好. 我以前也是这样做的.

另外可以使用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());
   
  }

}

2010年5月12日 17:55
0 0


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


// Java读取后,顺序已经反了
int javaReadInt = ;

// 将每个字节取出来
byte byte4 = (byte) (javaReadInt & 0xff);
byte byte3 = (byte) ((javaReadInt & 0xff00) >> 8);
byte byte2 = (byte) ((javaReadInt & 0xff0000) >> 16);
byte byte1 = (byte) ((javaReadInt & 0xff000000) >> 24);

// 拼装成 正确的int
int realint = (byte1& 0xff)<<0  + (byte2& 0xff)<<8 + (byte3& 0xff)<< 16 +(byte4& 0xff)<<24 ;

2010年5月12日 13:30
0 0


简单呀,用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中的整数了。


PS:如果我对C++中的整型数据的写入顺序猜测不对的话,你可以按照上述思路。

2010年5月12日 12:09
0 0

上次你好像也问了这个的呀,现在我们需要从一个C/C++语言生成的二进制文件中读出一个float数据

// 参见java.io.DataInputStream
// C++写入的字节顺序是从低到高(左低到右高),
   而java.io.DataInputStream读取的数据是从高到低(左高到右低)
// 所以需要自己改写一下
// 功能和java.io.DataInputStream类似的
public class CppInputStream extends FilterInputStream {

  public CppInputStream(InputStream in) {
    super(in);
  }
  public final int read(byte b[]) throws IOException {
    return in.read(b, 0, b.length);
  }

  public final int read(byte b[], int off, int len) throws IOException {
    return in.read(b, off, len);
  }

  public final void readFully(byte b[]) throws IOException {
    readFully(b, 0, b.length);
  }

  public final void readFully(byte b[], int off, int len) throws IOException {
    if (len < 0)
      throw new IndexOutOfBoundsException();
    int n = 0;
    while (n < len) {
      int count = in.read(b, off + n, len - n);
      if (count < 0)
        throw new EOFException();
      n += count;
    }
  }

  public final int skipBytes(int n) throws IOException {
    int total = 0;
    int cur = 0;
    while ((total < n) && ((cur = (int) in.skip(n - total)) > 0)) {
      total += cur;
    }
    return total;
  }

  public final byte readByte() throws IOException {
    int ch = in.read();
    if (ch < 0)
      throw new EOFException();
    return (byte) (ch);
  }

  public final int readUnsignedByte() throws IOException {
    int ch = in.read();
    if (ch < 0)
      throw new EOFException();
    return ch;
  }

  public final short readShort() throws IOException {
    int ch2 = in.read();
    int ch1 = in.read();
    if ((ch1 | ch2) < 0)
      throw new EOFException();
    return (short) ((ch1 << 8) + (ch2 << 0));
  }

  public final int readUnsignedShort() throws IOException {
    int ch2 = in.read();
    int ch1 = in.read();
    if ((ch1 | ch2) < 0)
      throw new EOFException();
    return (ch1 << 8) + (ch2 << 0);
  }

  public final char readChar() throws IOException {
    int ch2 = in.read();
    int ch1 = in.read();
    if ((ch1 | ch2) < 0)
      throw new EOFException();
    return (char) ((ch1 << 8) + (ch2 << 0));
  }

  public final int readInt() throws IOException {
    int ch4 = in.read();
    int ch3 = in.read();
    int ch2 = in.read();
    int ch1 = in.read();
    if ((ch1 | ch2 | ch3 | ch4) < 0)
      throw new EOFException();
    return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
  }

  private byte readBuffer[] = new byte[8];

  public final long readLong() throws IOException {
    readFully(readBuffer, 0, 8);
    return (((long) readBuffer[7] << 56) + ((long) (readBuffer[6] & 255) << 48)
        + ((long) (readBuffer[5] & 255) << 40) + ((long) (readBuffer[4] & 255) << 32)
        + ((long) (readBuffer[3] & 255) << 24) + ((readBuffer[2] & 255) << 16)
        + ((readBuffer[1] & 255) << 8) + ((readBuffer[0] & 255) << 0));
  }

  public final float readFloat() throws IOException {
    return Float.intBitsToFloat(readInt());
  }

  public final double readDouble() throws IOException {
    return Double.longBitsToDouble(readLong());
  }
}

2010年5月12日 11:18

相关推荐

Global site tag (gtag.js) - Google Analytics