how to convert c struct to java classes

Discussion in 'Java' started by warrior, Dec 23, 2009.

  1. warrior

    warrior Guest

    1) how to convert c struct to java classes
    2) How do i code the function pointers wriiten in c to java?
     
    warrior, Dec 23, 2009
    #1
    1. Advertising

  2. warrior

    Roedy Green Guest

    On Wed, 23 Dec 2009 02:03:25 -0800 (PST), warrior
    <> 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)
     
    Roedy Green, Dec 23, 2009
    #2
    1. Advertising

  3. warrior

    markspace Guest

    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.
     
    markspace, Dec 23, 2009
    #3
  4. warrior

    Arne Vajhøj Guest

    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
     
    Arne Vajhøj, Jan 2, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RA Scheltema
    Replies:
    3
    Views:
    414
    RA Scheltema
    Jan 6, 2004
  2. Gunnar G

    struct in struct

    Gunnar G, May 31, 2004, in forum: C++
    Replies:
    14
    Views:
    825
  3. DanielEKFA
    Replies:
    8
    Views:
    631
    DanielEKFA
    May 16, 2005
  4. Chris Fogelklou
    Replies:
    36
    Views:
    1,432
    Chris Fogelklou
    Apr 20, 2004
  5. Samuel R. Neff
    Replies:
    0
    Views:
    1,089
    Samuel R. Neff
    Jan 2, 2008
Loading...

Share This Page