Java 读取 C++写入的二进制数据

C++写入二进制文件时它的字节顺序是左地右高,而java则是左高右低,因此在读取数据时需要转换下。

转换代码如下:

public static int C2J_Int( int num ){
		System.out.println( "num: "+Integer.toBinaryString(num) );
		int value = 0;
		int i0 = num >> 24 & 0xff;
		int i1 = (num >> 8) & 0xff00;
		int i2 = (num << 8) & 0xff0000;
		int i3 = (num << 24) & 0xff000000;
		
		value = i0|i1|i2|i3;
		return value;
}



public static short C2J_Short( short num ){
		int value = 0;
		int i0 = (num >> 8) & 0xff;
		int i1 = (num << 8) & 0xff00;
		value = i0 | i1;
		return (short)value;
	}


C++代码部分:包含将数据写入文件和读取文件。

void writeData(){//写入数据

	ofstream fout;
	fout.open(filePath, ofstream::binary);
	int n = 1000;
	fout.write( (char *)&n, sizeof(n) );

	short m = 10;
	fout.write( (char *)&m, sizeof(m) );

	char msg[100] = "你好吗我爱你哈啊 Hello C++ 哈哈啊啊哈哈啊哈哈";
	int size = sizeof(msg);
	fout.write( (char*)&size, sizeof(int) );
	cout << "msg size: " << size << endl;
	fout.write( (char *)&msg, size );

	fout.close();
	cout << "数据导入成功" << endl;
	cout << "读取数据中..." << endl;
	cout << "size: " << sizeof(int) << endl;
	cout << "sizeof(str): "<< sizeof(msg) << endl;
	cout << strlen(msg) << endl;



}

void readData(){//读取数据

	ifstream fin;
	fin.open( filePath, ifstream::binary );
	int m = 0;
	fin.read( (char *)&m, sizeof(int) );
	cout << "m: " << m << endl;
	short n;
	fin.read( (char *)&n, sizeof(short) );
	cout << "n: " << n << endl;
	int size = 0;
	fin.read( (char*)&size, sizeof(int) );
	char msg[100] = "";//暂时不知道怎样让msg数组为动态数组通过size来初始化char数组大小,如果字串较长的话需要注意初始化
	fin.read( (char *)&msg, sizeof(msg) );
	cout << "msg: " << msg << endl;

	fin.close();
}

JAVA 代码部分:读取C++写入的二进制流文件

import java.io.File;
import java.io.FileInputStream;
import java.io.DataInputStream;

public class Main {

	public static void main( String args[] ){
		readData();
	}
	
	public static void readData(){
		System.out.println( "读取数据中..." );
		try {
			FileInputStream fis = new FileInputStream( new File("/789.dat") );
			DataInputStream dis = new DataInputStream( fis );
			
			System.out.println(dis.available());
			
			int m = dis.readInt();
			System.out.println(C2J_Int(m));
			short n = dis.readShort();
			System.out.println(C2J_Short(n));
			String str = C2J_String( dis );
			System.out.println( "str: "+str );
			
			dis.close();
			fis.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	public static int C2J_Int( int num ){
		
		System.out.println( "num: "+Integer.toBinaryString(num) );
		int value = 0;
		int i0 = num >> 24 & 0xff;
		int i1 = (num >> 8) & 0xff00;
		int i2 = (num << 8) & 0xff0000;
		int i3 = (num << 24) & 0xff000000;
		
		value = i0|i1|i2|i3;
		return value;
	}
	
	public static short C2J_Short( short num ){
		int value = 0;
		int i0 = (num >> 8) & 0xff;
		int i1 = (num << 8) & 0xff00;
		value = i0 | i1;
		return (short)value;
	}
	
	public static String C2J_String( DataInputStream dis ){
		String result ="";
		byte buffer[] = null;
		try {
			int size = C2J_Int(dis.readInt());//读取字符串长度
			buffer = new byte[size];
			dis.read(buffer);
			result = new String( buffer, "gb2312" );
			System.out.println( "result: "+result );
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	
	
	
}












  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 C++ 读取写入二进制数据文件(.dat)可以使用流(stream)类来实现。具体步骤如下: 1. 打开文件:使用 std::fstream 类的 open() 函数打开文件,指定文件名和打开模式(输入、输出、二进制等)。 2. 写入数据:使用流对象的 write() 函数将数据写入文件。write() 函数需要传入两个参数:第一个参数是指向要写入数据的指针,第二个参数是要写入的字节数。 3. 读取数据:使用流对象的 read() 函数从文件读取数据。read() 函数需要传入两个参数:第一个参数是指向要读取数据的指针,第二个参数是要读取的字节数。 4. 关闭文件:使用流对象的 close() 函数关闭文件。 下面是一个简单的示例代码,演示如何使用流类读取写入二进制数据文件: ```c++ #include <iostream> #include <fstream> using namespace std; struct Person { char name[20]; int age; }; int main() { // 打开文件 fstream file("person.dat", ios::binary|ios::in|ios::out); // 写入数据 Person p1 = {"Tom", 20}; file.write((char*)&p1, sizeof(p1)); // 读取数据 file.seekg(0, ios::beg); // 将文件指针移动到文件开头 Person p2; file.read((char*)&p2, sizeof(p2)); cout << "Name: " << p2.name << endl; cout << "Age: " << p2.age << endl; // 关闭文件 file.close(); return 0; } ``` 在上面的代码中,我们使用了一个结构体 Person 来存储数据。首先,我们创建了一个 fstream 对象来打开文件 "person.dat",并指定了打开模式为二进制输入输出模式。然后,我们使用 write() 函数将一个 Person 对象写入文件中。接着,我们使用 seekg() 函数将文件指针移动到文件开头,然后使用 read() 函数从文件读取一个 Person 对象,并将其存储到 p2 变量中。最后,我们输出了读取到的数据,并关闭了文件。 注意,在使用文件读取写入二进制数据时,需要使用 char* 类型的指针来强制转换数据类型。此外,还需要使用 seekg() 函数来移动文件指针的位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值