-
c++和java字节高低位的转换3
// 参见java.io.DataInputStream
// C++写入的字节顺序是从低到高(左低到右高),
而java.io.DataInputStream读取的数据是从高到低(左高到右低)
现在需要用java读取一个由c++生成的二进制文件中的int型数据、可是c++和java的字节高地位顺序不一样、需要转换有谁能知道怎么转换
2010年5月12日 11:14
5个答案 按时间排序 按投票排序
-
可以使用commons-io中的类SwappedDataInputStream
commons-io包可以到apache上下载2010年5月13日 21:41
-
"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
-
也可以先用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
-
简单呀,用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
-
上次你好像也问了这个的呀,现在我们需要从一个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
相关推荐
资源为32位机下高低位字节交换类,适用于c++语法下的环境,一般跨平台通讯程序时,涉及主机字节不一致情况,如Linux下的服务程序和windows下客户端程序通讯时,有可能需要做主机字节高低位交换,本类提供int、float...
C++转换JAVA工具,安装工具,验证可用
c++ 转换 java (试用版) c++ 转换 c# (正式版)
Tangible Software Solutions 是功能齐全的源代码转换软件,可在 VB.NET、C#、Java、C++和Python之间,使用简单,功能丰富,灵活的进行选择和操作,界面友好。 C# to C++ 转换 C# to Java 转换 C# to VB 转换 C++ ...
C++代码转java工具 C++代码转java工具 C++代码转java工具
java c++ socket java c++ socket java c++ socket java c++ socket java c++ socket java c++ socket java c++ socket java c++ socket java c++ socket java c++ socket java c++ socket
C++字节迭代器,一个把可字节化的对象转换为字节序列的中间层。
java jni 与 c++ 数据类型转换 及调用
包括了基本的单字节、多字节、双字节之间的相互转换。如果想对版本升级及学习可以参考。(从他人文章中下载的,希望没有侵权)。
超强的C++ to Java转换工具!!希望对大家有帮助。无污染。
C++ 网络字节顺序转换函数、ip格式转换函数
Android Studio项目,此Demo实现Java调用C++函数,然后C++函数回调Java方法、纯C++直接调用Java方法,此为github地址链接
基于tcp通讯,涉及java的netty服务器的推送功能和c++socket的封装以及protobuf在java和c++中的使用。
CPlus_to_Java_Converter(C++转java);C++转java软件
通过代码和问题详细说明c++和java互调方法
C++和Java多态的区别
《算法设计、分析与实现:c、c++和java》特色是按照算法之间逻辑关系编排学习顺序,并对每一个经典算法,都给出了完整的c/c++/java三种主流编程语言的实现程序,是一本既能让读者清晰、轻松地理解算法思想,又能让...
汇编语言与计算机体系结构:使用C++和Java(Assembly Language and Computer Architecture Using C++ and Java) 配套代码
C++转JAVA的转换方法及约定[整理].pdf
在Qt平台C++和Java代码的交互。更多Qt文章,请看http://blog.csdn.net/u014597198/article/category/5692117