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());
}
}
java读取C++结构体,类型转换相关推荐
- Java中发送结构体
http://blog.csdn.net/xwchen/article/details/1585908 最近给个朋友做个网站的客户端,使用C/S模式,Client为VC6开发,Server为Java, ...
- java和C结构体通信
主要是看代码分析别人写的发送结构体思路,如内存补0操作: package com.cvicse.naba.service.impl.user; import java.util.ArrayList; ...
- c 语言结构体类型转换,C 语言编程 — 结构体的数据类型转换
目录 数组类型强制类型转换为结构体 先看一个例子: #include int main(void) { unsigned char arr[] = "0123456789abcdefghij ...
- 关于Python读取matlab结构体数据
如下图所示,nclcolormap是拥有248个字段的结构体下面使用scipy可将任意字段读取import scipy.io as sciodataFile = r'D:\我的文件\我的数据\nclc ...
- java 链表放置结构体_结构体和它在链表中的使用
一.结构体 由不同类型的数据组合成一个整体,以便引用,这些组合在一个整体中的数据是互相联系的. 1.1如何声明结构体呢? struct 结构体名 //结构体名字用作结构体类型的标志 {成员列表}; ...
- java 读取数据库结构_JAVA从SQLITE数据库中读取省份地市构造一棵树
我们向ac_device.s3db数据库文件中的ac_device表中插入省份名称.地市名 称.IP地址和用户名及密码内容,可以使用 insert语句进行操作.如下所示: stmt.execute( ...
- JNA参数类型转换(含接收、发送结构体)——JNA-JNI(五)
JNA参数类型转换(含接收.发送结构体)--JNA-JNI(五) 系列文章: Java通过JNI调用C++动态链接库dll,并打在jar包内 --JNA-JNI(一) Java使用JNA调用C++动态 ...
- 【C 语言】文件操作 ( 读取文件中的结构体数组 | feof 函数使用注意事项 )
文章目录 一.读取文件中的结构体数组 | feof 函数使用注意事项 二.代码示例 一.读取文件中的结构体数组 | feof 函数使用注意事项 读取文件结构体时 , 可以循环读取文件中的数据 , 只使 ...
- Java实现结构体,让字节流封送简单起来
和C/C++不同,Java中没有结构体,联合体.这是Java作为高级语言的进步,但也使得它在进行网络字节流封送上显得捉襟见肘. 这里所指的是在进行网络编程,串口编程等需要和某些C语言中结构体形式定义的 ...
最新文章
- 重走JAVA之路(一):复盘ButterKnife-编译时注解
- MySQL学习(十五)
- CVPR2021 论文大盘点:全景分割论文汇总(共15篇)
- JVM学习笔记之-执行引擎(Execution Engine)
- Nginx中如何配置中文域名?
- 一网打尽中文编码转换---6种编码30个方向的转换
- ITU衡量信息社会报告:我国ICT发展指数进入亚太前十
- 对象的使用 java 1613806439
- Window系统 安装TFLearn
- 物联卡与SIM卡相比优势在哪
- sprintboot 发布
- 智驾科技MAXIEYE完成3亿元B轮融资,暂未取得品牌同名商标
- SEM和SEO的区别?哪个更好
- html页面排版会乱,窗口缩放导致页面排版错乱的解决方法
- unity显示FPS
- Leetcode刷题114. 二叉树展开为链表
- 征信衍生、信用卡数据场景如何做好分析,这一种时序特征方法得学学|金融信用分析师必学内容
- bigemap手机版app如何加载矢量数据
- 微信演进的六点思考:微信生态如何演变?如何在其中掘金?
- 一元多项式式计算器(哈工大数据结构实验)
热门文章
- 泛广电领域的卫星传输和公网传输
- Hadoop之Hadoop序列化
- 大牛书单 | 读书日,他们最近看了这些书
- Golang 单元测试详尽指引
- Nginx HTTP之请求行解析函数ngx_http_parse_request_line
- mpeg4ip.h:126: error: new declaration ‘char* strcasestr(const char*, const char*)’
- PyTorch GPU安装指南 (Ubuntu 16.04 anaconda cuda8.0 cuDNN6.0)
- 大剑无锋之new一个对象背后发生了什么?
- 将d:\java目录下的所有.java文件复制到d:\jad 目录下,并将原来文件的扩展名从.java 改为.jad
- leetcode 994. Rotting Oranges | 994. 腐烂的橘子(BFS)