在Java中使用C结构 [英] Using C struct in Java

查看:100
本文介绍了在Java中使用C结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写一个Java程序,它将从网络接收消息并将其内容显示给用户。问题是我收到的消息只是C结构的二进制转储。除此之外,一些消息来自小端机器,一些来自大端,没有字段被转换为网络字节顺序。
我的一种方法是使用JNI并将c结构转换为某个XML字符串,然后将此XML字符串反序列化为Java对象。这是一项艰巨的工作,因为大约有122种不同的结构,每种结构都包含20多个区域。
我想知道是否有一个库/工具/方法可以让我的工作变得容易一些?

I have to code a Java program that will receive messages from network and display their content to the user. The problem is that the messages that I receive are simply binary dumps of C structures. Add to this some of the messages are coming from little endian machines and some from big endian without the fields being converted to network byte order. One way I have is to use JNI and convert c structs to some XML string and then de serialize this XML string to a Java Object. This is a laborous job since there about 122 different structs and each one of them contains more than 20 fields. I wonder if there is a library/tool/methodology which could make my job a bit easy ?

推荐答案

< a href =http://www.swig.org =nofollow noreferrer> Swig 将在将C结构映射到Java对象方面为您处理大量繁琐的重复工作。查看 Swig / Java手册包装C结构

Swig will handle a lot of the tedious repetitive work for you in terms of mapping the C structs to Java objects. Check out the Swig/Java manual and the entry on wrapping C structures.

这篇关于在Java中使用C结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆