Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how to convert c struct to java classes

Reply
Thread Tools

how to convert c struct to java classes

 
 
warrior
Guest
Posts: n/a
 
      12-23-2009
1) how to convert c struct to java classes
2) How do i code the function pointers wriiten in c to java?
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      12-23-2009
On Wed, 23 Dec 2009 02:03:25 -0800 (PST), warrior
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>1) how to convert c struct to java classes

see http://mindprod.com/project/readc.html
http://mindprod.com/project/structreaderamanuensis.html

>2) How do i code the function pointers wriiten in c to java?

see http://mindprod.com/jgloss/delegate.html
http://mindprod.com/jgloss/callback.html

--
Roedy Green Canadian Mind Products
http://mindprod.com
If you think it’s expensive to hire a professional to do the job, wait until you hire an amateur.
~ Red Adair (born: 1915-06-18 died: 2004-08-07 at age: 89)
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      12-23-2009
warrior wrote:
> 1) how to convert c struct to java classes



Use a class. Consider giving it public members to mimic the
accessibility of C structs, but also consider giving it private
variables and public accessors and mutator methods.


> 2) How do i code the function pointers wriiten in c to java?


The easiest is to just use the reference to the object, which you
already have. All objects in Java that you deal with are actually
"pointers" (references) anyway, you have no choice but to use a "pointer."

In some instances, you may wish to define an interface or use
reflection. This sounds a bit advanced for you right now, I'd try the
reference described above first.

 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      01-02-2010
On 23-12-2009 05:03, warrior wrote:
> 1) how to convert c struct to java classes


Multiple ways.

DataInputStream wrapped around a ByteArrayInputStream wrapped
around the byte array.

NIO ByteBuffer wrapped around the byte array.

More custom solution. Example:

import dk.vajhoej.record.Alignment;
import dk.vajhoej.record.Endian;
import dk.vajhoej.record.FieldType;
import dk.vajhoej.record.Struct;
import dk.vajhoej.record.StructField;

/*
* struct data
* {
* long int liv;
* int bv1 : 4;
* int bv2 : 4;
* short int siv;
* };
*
* With a compiler and settings that uses little endian, natural
* alignment, sizeof(long)=4, sizeof(short)=2 etc..
*/
@Struct(endianess=Endian.LITTLE, alignment=Alignment.NATURAL)
public class Data {
@StructField(n=0,type=FieldType.INT4)
private int liv;
@StructField(n=1,type=FieldType.BIT,length=4)
private int bv1;
@StructField(n=2,type=FieldType.BIT,length=4)
private int bv2;
@StructField(n=3,type=FieldType.INT2)
private int siv;
public int getLiv() {
return liv;
}
public int getBv1() {
return bv1;
}
public int getBv2() {
return bv2;
}
public int getSiv() {
return siv;
}
}

import dk.vajhoej.record.RecordException;
import dk.vajhoej.record.StructReader;

public class CStruct {
public static void main(String[] args) throws RecordException {
byte[] b = { 7, 0, 0, 0, 0x12, 0, 3, 0 };
StructReader sr = new StructReader(b);
Data o = sr.read(Data.class);
System.out.println(o.getLiv() + " " + o.getBv1() + " " +
o.getBv2() + " " + o.getSiv());
}
}

> 2) How do i code the function pointers wriiten in c to java?


The Java way is to have sub classes that overrides a certain
method with the implementation you need.

Arne
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
"Convert to Web Application" didn't convert pages to partial classes in 1.1 -> 3.5 migration Samuel R. Neff ASP .Net 0 01-02-2008 03:38 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
inner classes in python as inner classes in Java Carlo v. Dango Python 14 10-19-2003 08:49 AM



Advertisments