您的位置:首页 > 编程语言 > C语言/C++

JAVA 与C语言中struct union 字节流转换方案 Javolution

2013-10-10 14:36 501 查看
Javolution

Javolution提供Struct和Union类具有直接使用C/C++应用程序的协同工作的能力.

Here
is an example of C/C++ struct:
struct
UDPMessage {

unsigned short year;

unsigned char month;

unsigned char day;

};

struct Student {

char name[64];

struct
UDPMessage birth;

float grades[10];

Student* next;

};


and here is the Java equivalent using Javolution 2.2.5:

public static class Date extends Struct {

public final Unsigned16 year = new Unsigned16();

public final Unsigned8 month = new Unsigned8();

public final Unsigned8 day = new Unsigned8();

}

public static class Student extends Struct {

public final Utf8String name = new Utf8String(64);

public final Date birth = (Date) inner(new Date());

public final Float32[] grades = (Float32[]) array(new Float32[10]);

public final Reference32 next = new Reference32(Student.class);

}


public void testJavolution(){

UDPMessage message = new UDPMessage();

message.year.set(2);

message.month.set( (short) 3);

message.day.set((short) 5);

System.out.println(bytes2HexStr(message.getByteBuffer().array()," "));

byte[] bytes=new byte[4];

for(int i=0;i<4;i++){

bytes[i]=(byte) i;

}

message.setByteBuffer(ByteBuffer.wrap(bytes),0);

System.out.println(":: day="+message.day.get()+" month="+message.month.get()+" year="+message.year.get());

}

Multi-dimensional arrays of struct/union or of primitive types are also supported.

Struct API: http://javolution.org/api/javolution/io/Struct.html

jar download:

http://www.java2s.com/Code/Jar/j/Downloadjavolution541jar.htm

api doc:

http://javolution.org/target/site/apidocs/javolution/io/Struct.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: