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

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


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值