java读取C++结构体,类型转换

package com.struct.method;

import java.io.EOFException;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

//参见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());
	}
}

也可以先用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 ;

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

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值