Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to convert c struct to java classes

25 views
Skip to first unread message

warrior

unread,
Dec 23, 2009, 5:03:25 AM12/23/09
to
1) how to convert c struct to java classes
2) How do i code the function pointers wriiten in c to java?

Roedy Green

unread,
Dec 23, 2009, 11:20:26 AM12/23/09
to
On Wed, 23 Dec 2009 02:03:25 -0800 (PST), warrior
<chandu...@gmail.com> 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)

markspace

unread,
Dec 23, 2009, 12:00:59 PM12/23/09
to
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.

Arne Vajhøj

unread,
Jan 1, 2010, 8:46:06 PM1/1/10
to
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

Mark

unread,
Jan 5, 2010, 7:05:31 AM1/5/10
to
On Wed, 23 Dec 2009 02:03:25 -0800 (PST), warrior
<chandu...@gmail.com> wrote:

>1) how to convert c struct to java classes

I did this by using C code with JNI to create java classes. All the
code was automatically generated while parsing the C code.

--
(\__/) M.
(='.'=) Due to the amount of spam posted via googlegroups and
(")_(") their inaction to the problem. I am blocking most articles
posted from there. If you wish your postings to be seen by
everyone you will need use a different method of posting.
[Reply-to address valid until it is spammed.]

0 new messages