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:
and here is the Java equivalent using Javolution 2.2.5:
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
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
相关文章推荐
- Javolution 2.2.5 - Java Struct/Union Simplified
- C语言的struct/union字节对齐详解
- 对《C语言:内存字节对齐详解——struct 和 union 》的理解
- C_C语言的enum、struct、union的使用详解
- C语言中的struct、union、enum、Typedef
- java 简单的字节流与字符串的转换换 及加密与解密(对称)
- Java:switch语句与c语言的不同,大写转换方法
- JAVA字节流与字符流的相互转换
- c语言中的结构(struct)和联合(union)简介
- Java基础学习总结(14)---File类、字节流与字符流、字节字符转换流
- C语言实现字节流与十六进制字符串的相互转换
- javolution.io.Struct序列化C语言数据结构
- atitit.商业版 源码保护 与 java本地原生代码转换 的方案总结
- C语言多继承时子类与基类指针关系,union与struct区别,volatile修饰符作用
- 字节流、字符串、16进制字符串转换__Java(转)
- Java中字节流、字符流、转换流
- [转载] c语言中的结构(struct)和联合(union)简介
- 【java基础】采用字节流的文件IO演示,字节流与字符流转换,转换流的编码解码
- JAVA中的字节流和字符流的转换
- 字节流、字符串、16进制字符串转换__Java(转)