Re: Reading a C struct in java

Discussion in 'Java' started by Lothar Kimmeringer, Sep 29, 2009.

  1. Mark wrote:

    > I am writing an app in java which reads data from a socket from a C
    > language program. The data is encoded as a C struct of char arrays
    > something like this;
    >
    > typedef struct {
    > char type[1];
    > char length[6];
    > char acknowledge[1];
    > char duplicate[1];
    > ...
    > } type_t;
    >
    > How can I decode a structure like this in Java without using JNI (a
    > requirement)?


    Do you need a parser creating Java-classes or do you simply
    want to read in the data? If the latter you have to find out
    the endianess of the system if you have to read in data consisting
    of more than one byte (e.g. int) and how many bytes an int
    has (varies in dependence of the processor architecture).
    The rest can be done with simply reading in from the stream

    Above struct can be read the following way:

    InputStreamReader is = new InputStreamReader(socket.getInputStream(),
    "8859_1");
    int type = is.read();
    char[] length = new char[6];
    is.read(length);
    int acknowledge = is.read();
    int duplicate = is.read();

    this assumes that you only have chars in that struct (i.e.
    unsigned bytes). If you have mixed types you should use an
    InputStream and create String out of the byte-array:

    InputStream is = socket.getInputStream();
    int type = is.read();
    byte[] buf = new byte[6];
    is.read(buf);
    String length = new String(buf, 0, 6, "8859_1");
    int acknowledge = is.read();
    int duplicate = is.read();
    ....

    If you need a parser you should check out Google. There are a
    couple of results but I didn't take the time to check them in
    detail, so I can't suggest anything.


    Regards, Lothar
    --
    Lothar Kimmeringer E-Mail:
    PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

    Always remember: The answer is forty-two, there can only be wrong
    questions!
     
    Lothar Kimmeringer, Sep 29, 2009
    #1
    1. Advertising

  2. On Sep 29, 11:33 am, Lothar Kimmeringer <>
    wrote:
    > Mark wrote:
    > > I am writing an app in java which reads data from a socket from a C
    > > language program.  The data is encoded as a C struct of char arrays
    > > something like this;

    >
    > > typedef struct {
    > >     char type[1];
    > >     char length[6];
    > >     char acknowledge[1];
    > >     char duplicate[1];
    > >     ...
    > > } type_t;

    >
    > > How can I decode a structure like this in Java without using JNI (a
    > > requirement)?

    >
    > Do you need a parser creating Java-classes or do you simply
    > want to read in the data? If the latter you have to find out
    > the endianess of the system if you have to read in data consisting
    > of more than one byte (e.g. int) and how many bytes an int
    > has (varies in dependence of the processor architecture).
    > The rest can be done with simply reading in from the stream
    >
    > Above struct can be read the following way:
    >
    > InputStreamReader is = new InputStreamReader(socket.getInputStream(),
    >    "8859_1");
    > int type = is.read();
    > char[] length = new char[6];
    > is.read(length);
    > int acknowledge = is.read();
    > int duplicate = is.read();


    Why use a Reader? A C struct is a pack of bytes, not characters. Also,
    Java chars are not ASCII chars and are thus not represented as a
    single byte, so if the struct used "char" to mean "byte", you might
    want to use a byte in Java, with the caveat that Java bytes are always
    signed.

    > this assumes that you only have chars in that struct (i.e.
    > unsigned bytes). If you have mixed types you should use an
    > InputStream and create String out of the byte-array:


    Again, why create a String if you are dealing with an array of bytes?
    If you deal with C strings, you have to take into account the \0
    string terminator, but this is not the case of the OP, as far as I
    understand.

    > InputStream is = socket.getInputStream();
    > int type = is.read();
    > byte[] buf = new byte[6];
    > is.read(buf);
    > String length = new String(buf, 0, 6, "8859_1");
    > int acknowledge = is.read();
    > int duplicate = is.read();
    > ...
    >
    > If you need a parser you should check out Google. There are a
    > couple of results but I didn't take the time to check them in
    > detail, so I can't suggest anything.
    >
    > Regards, Lothar
    > --
    > Lothar Kimmeringer                E-Mail:
    >                PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
    >
    > Always remember: The answer is forty-two, there can only be wrong
    >                  questions!
     
    Alessio Stalla, Sep 29, 2009
    #2
    1. Advertising

  3. Lothar Kimmeringer

    Lew Guest

    Mark wrote:
    > On Tue, 29 Sep 2009 11:33:29 +0200, Lothar Kimmeringer
    > <> wrote:
    >
    >> Mark wrote:
    >>
    >>> I am writing an app in java which reads data from a socket from a C
    >>> language program. The data is encoded as a C struct of char arrays
    >>> something like this;
    >>>
    >>> typedef struct {
    >>> char type[1];
    >>> char length[6];
    >>> char acknowledge[1];
    >>> char duplicate[1];
    >>> ...
    >>> } type_t;
    >>>
    >>> How can I decode a structure like this in Java without using JNI (a
    >>> requirement)?

    >> Do you need a parser creating Java-classes or do you simply
    >> want to read in the data?

    >
    > I don't want a parser. The exact structure is known at compile time.
    >
    >> If the latter you have to find out
    >> the endianess of the system if you have to read in data consisting
    >> of more than one byte (e.g. int) and how many bytes an int
    >> has (varies in dependence of the processor architecture).

    >
    > Endian-ness is not an issue. All the fields are char arrays.
    >
    >> The rest can be done with simply reading in from the stream

    >
    > I don't have access to the stream. The data arrives to my code in
    > already in a byte array.
    >
    > I guess I want to know if there is an easy way to map the C structure
    > to a java class or whether I will have to dissect the byte array
    > completely manually.


    You have to control how the C struct is written. Otherwise things like the
    padding between struct elements can vary and trip you up.

    --
    Lew
     
    Lew, Sep 29, 2009
    #3
  4. On Sep 29, 1:10 pm, Mark <>
    wrote:
    > On Tue, 29 Sep 2009 11:33:29 +0200, Lothar Kimmeringer
    >
    >
    >
    > <> wrote:
    > >Mark wrote:

    >
    > >> I am writing an app in java which reads data from a socket from a C
    > >> language program.  The data is encoded as a C struct of char arrays
    > >> something like this;

    >
    > >> typedef struct {
    > >>     char type[1];
    > >>     char length[6];
    > >>     char acknowledge[1];
    > >>     char duplicate[1];
    > >>     ...
    > >> } type_t;

    >
    > >> How can I decode a structure like this in Java without using JNI (a
    > >> requirement)?

    >
    > >Do you need a parser creating Java-classes or do you simply
    > >want to read in the data?

    >
    > I don't want a parser.  The exact structure is known at compile time.


    ....by the C compiler, and by you. However since a C struct is really
    an unstructured byte array, you need a parser to extract its parts, or
    if you prefer a Java class with methods like

    public char getType() {
    return (char) myStructArray[0];
    }

    public void setType(char c) {
    myStructArray[0] = (byte) c;
    }

    > >If the latter you have to find out
    > >the endianess of the system if you have to read in data consisting
    > >of more than one byte (e.g. int) and how many bytes an int
    > >has (varies in dependence of the processor architecture).

    >
    > Endian-ness is not an issue.  All the fields are char arrays.  
    >
    > >The rest can be done with simply reading in from the stream

    >
    > I don't have access to the stream.  The data arrives to my code in
    > already in a byte array.


    You can create a stream that reads from a byte array:
    java.io.ByteArrayInputStream.

    > I guess I want to know if there is an easy way to map the C structure
    > to a java class or whether I will have to dissect the byte array
    > completely manually.


    The second one you said. In Java, you have no way to know how classes
    are laid out in memory, and you cannot take a byte array and interpret
    it as a Java object like C does.

    What you could do in principle - if you had to map a huge number of C
    structs - would be to write a translator that, statically, read the
    struct declarations from C code and automatically generated Java
    classes with methods to parse the structures and access their fields.

    Alessio
     
    Alessio Stalla, Sep 29, 2009
    #4
  5. Lothar Kimmeringer

    Roedy Green Guest

    On Tue, 29 Sep 2009 11:33:29 +0200, Lothar Kimmeringer
    <> wrote, quoted or indirectly quoted someone
    who said :

    >
    >InputStreamReader is = new InputStreamReader(socket.getInputStream(),
    > "8859_1");
    >int type = is.read();
    >char[] length = new char[6];
    >is.read(length);
    >int acknowledge = is.read();
    >int duplicate = is.read();


    and if it is binary data, create a great string of
    DataInputStream.readInt calls or LEDataInputSTream.readInt calls (for
    little endian). You must skip any padding or alignment bytes C
    inserted.

    Ugly, You betcha as the lady from Alaska would say.

    You might write a program that parsed the C source code for the struct
    and generated the Java source.

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


    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    On two occasions I have been asked [by members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
    ~ Charles Babbage (born: 1791-12-26 died: 1871-10-18 at age: 79)
     
    Roedy Green, Sep 29, 2009
    #5
  6. Lothar Kimmeringer

    Roedy Green Guest

    On Tue, 29 Sep 2009 12:10:14 +0100, Mark
    <> wrote, quoted or indirectly
    quoted someone who said :

    >I don't have access to the stream. The data arrives to my code in
    >already in a byte array.


    You presumably have a map of where various 8-bit strings start and end
    in that byte[]. Your job is convert each 8-bit string to a java 16-bit
    string with whatever encoding the C program that wrote them originally
    used.

    see http://mindprod.com/jgloss/conversion.html#BYTETOSTRING
    http://mindprod.com/jgloss/encoding.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    On two occasions I have been asked [by members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
    ~ Charles Babbage (born: 1791-12-26 died: 1871-10-18 at age: 79)
     
    Roedy Green, Sep 29, 2009
    #6
  7. Lothar Kimmeringer

    Roedy Green Guest

    On Tue, 29 Sep 2009 12:10:14 +0100, Mark
    <> wrote, quoted or indirectly
    quoted someone who said :

    >>Do you need a parser creating Java-classes or do you simply
    >>want to read in the data?

    >
    >I don't want a parser. The exact structure is known at compile time.


    You still might want a parser, if there are great reams of this crud,
    or if you think you may get another such problem handed to you next
    month. The parser does not scan the data but the source code for the
    Struct.
    see http://mindprod.com/project/readc.html


    It could be a klutzy "parser" where the data is massaged in some way
    to make it easy to read, e.g. converted to CSV.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    On two occasions I have been asked [by members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
    ~ Charles Babbage (born: 1791-12-26 died: 1871-10-18 at age: 79)
     
    Roedy Green, Sep 29, 2009
    #7
  8. Alessio Stalla wrote:

    >> InputStreamReader is = new InputStreamReader(socket.getInputStream(),
    >>    "8859_1");
    >> int type = is.read();
    >> char[] length = new char[6];
    >> is.read(length);
    >> int acknowledge = is.read();
    >> int duplicate = is.read();

    >
    > Why use a Reader? A C struct is a pack of bytes, not characters. Also,
    > Java chars are not ASCII chars and are thus not represented as a
    > single byte, so if the struct used "char" to mean "byte", you might
    > want to use a byte in Java, with the caveat that Java bytes are always
    > signed.


    I was assuming that the data in the struct is actually text. So
    using a Reader allows you to create a String just by passing
    a char-array instead of doing a conversion from a byte-array
    to String by specifying the encoding over and over again.

    >> this assumes that you only have chars in that struct (i.e.
    >> unsigned bytes). If you have mixed types you should use an
    >> InputStream and create String out of the byte-array:

    >
    > Again, why create a String if you are dealing with an array of bytes?


    The time I was programming C is some years ago, but char-arrays
    (as being used in the example-struct) are in general used to
    represent (printable) text. So why _not_ using Strings if you
    have to deal with text?


    Regards, Lothar
    --
    Lothar Kimmeringer E-Mail:
    PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

    Always remember: The answer is forty-two, there can only be wrong
    questions!
     
    Lothar Kimmeringer, Sep 29, 2009
    #8
  9. Lothar Kimmeringer

    Roedy Green Guest

    On Tue, 29 Sep 2009 08:59:06 -0700, Roedy Green
    <> wrote, quoted or indirectly quoted
    someone who said :

    >You presumably have a map of where various 8-bit strings start and end
    >in that byte[]. Your job is convert each 8-bit string to a java 16-bit
    >string with whatever encoding the C program that wrote them originally
    >used.


    In addition, to the fixed size allocation marking the boundaries,
    there is a terminating null byte to mark the end of the string. You
    will have to scan for that to find the actual length of the string in
    bytes.

    It conceivable, though unlikely, there are no such null markers, and
    you have no multi-byte encodings. Then you could decode the entire
    byte[] struct to a String in one fell swoop, then use String.substring
    to pick out the fields. You are safer to extract the fields at the
    byte level. Then the code will work even if there are multi-byte
    encodings, and nulls.

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    On two occasions I have been asked [by members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
    ~ Charles Babbage (born: 1791-12-26 died: 1871-10-18 at age: 79)
     
    Roedy Green, Sep 29, 2009
    #9
  10. Lothar Kimmeringer

    Lew Guest

    Mark wrote:
    > All the C/C++ compilers we use have pragmas to force known
    > alignment/padding etc. Byte order is not an issue in this layer,
    > since all parameters are encoded in strings.


    And you think that makes you safe. Chuckle.

    If you succeed in writing a reader that counts on those pragma settings, you
    can count on someone sending it data that doesn't conform to them at some
    point in the probably-not-distant future. That stakeholder will then complain
    loud and long for an "enhancement" to deal with their data, and likely will
    refuse to use the "old" pragmas just to be compatible with the reader.

    Sometimes the overhead of writing the reader is low enough that you don't mind
    that scenario. You just write a new reader for the different settings and
    tell them to use that one instead.

    If you then encounter a third format, you might begin to lament the early
    decision to rely on one particular specific set of pragma settings.

    --
    Lew
     
    Lew, Sep 30, 2009
    #10
  11. bugbear wrote:
    > Mark wrote:
    >> I don't want a parser. The exact structure is known at compile time.

    >
    > Yes. Some parsers read fixed formats. No contradiction there.


    I am not convinced.

    I will not call fread for a parser because it can read
    a struct.

    http://en.wikipedia.org/wiki/Parser says:

    <quote>
    In computer science and linguistics, parsing, or, more formally,
    syntactic analysis, is the process of analyzing a text, made of a
    sequence of tokens (for example, words), to determine its grammatical
    structure with respect to a given (more or less) formal grammar.

    Parsing is also an earlier term for the diagramming of sentences of
    natural languages, and is still used for the diagramming of inflected
    languages, such as the Romance languages or Latin. The term parsing
    comes from Latin pars (ÅrÄtiÅnis), meaning part (of speech).[1][2]</quote>

    But it is obviously more terminology than software.

    Arne
     
    Arne Vajhøj, Sep 30, 2009
    #11
  12. Lothar Kimmeringer

    Arne Vajhøj Guest

    Mark wrote:
    > On Tue, 29 Sep 2009 13:46:32 +0100, bugbear
    > <bugbear@trim_papermule.co.uk_trim> wrote:
    >> Mark wrote:
    >>> This is already done. I just need to know how to get at the data and
    >>> make it useable in Java.
    >>>
    >>> I am an experienced programmer in C/C++ but am new to Java BTW.

    >> Just to be explicit, I guess the thing "in your head" is the C
    >> idiom read(stream, &mystruct, sizeof(mystruct)
    >>
    >> You definitely can't do this in Java, and (IMHO) you
    >> shouldn't do it in C.

    >
    > I know you can't do this in Java, hence my question. I'm beginning to
    > figure it out, but I am not yet familiar with all the Java classes ;-)


    ByteArrayInputStream and DataInputStream would be my suggestion for
    builtin Java functionality (*).

    Combined with simple code to convert endianess and String getBytes
    to convert bytes to String.


    Arne

    *) I do have an alternative that I will get back to later.
     
    Arne Vajhøj, Sep 30, 2009
    #12
  13. Lew wrote:
    > Mark wrote:
    >> All the C/C++ compilers we use have pragmas to force known
    >> alignment/padding etc. Byte order is not an issue in this layer,
    >> since all parameters are encoded in strings.

    >
    > And you think that makes you safe. Chuckle.
    >
    > If you succeed in writing a reader that counts on those pragma settings,
    > you can count on someone sending it data that doesn't conform to them at
    > some point in the probably-not-distant future. That stakeholder will
    > then complain loud and long for an "enhancement" to deal with their
    > data, and likely will refuse to use the "old" pragmas just to be
    > compatible with the reader.
    >
    > Sometimes the overhead of writing the reader is low enough that you
    > don't mind that scenario. You just write a new reader for the different
    > settings and tell them to use that one instead.
    >
    > If you then encounter a third format, you might begin to lament the
    > early decision to rely on one particular specific set of pragma settings.


    It makes him as safe as any other solution.

    There is a spec. You have some code that follow that spec.

    If someone is not following the spec and is able to force you to
    support their alternative spec, then you need to write some new code.

    It does not matter if it is C, Java or Cobol.

    Arne
     
    Arne Vajhøj, Sep 30, 2009
    #13
  14. Lothar Kimmeringer

    Eric Sosman Guest

    Mark wrote:
    > On Tue, 29 Sep 2009 13:46:32 +0100, bugbear
    > <bugbear@trim_papermule.co.uk_trim> wrote:
    >
    >> Mark wrote:
    >>
    >>> This is already done. I just need to know how to get at the data and
    >>> make it useable in Java.
    >>>
    >>> I am an experienced programmer in C/C++ but am new to Java BTW.

    >> Just to be explicit, I guess the thing "in your head" is the C
    >> idiom read(stream, &mystruct, sizeof(mystruct)
    >>
    >> You definitely can't do this in Java, and (IMHO) you
    >> shouldn't do it in C.

    >
    > I know you can't do this in Java, hence my question. I'm beginning to
    > figure it out, but I am not yet familiar with all the Java classes ;-)


    It's not a matter of memorizing a whole bucketload of Java
    classes, it's about realizing what a Java class *is*: A Java class
    is a collection of variables and methods and constructors. Period.

    There are a few subtleties in the above that might not jump
    out at you right away. One is the word "collection:" the code
    and the data are just "collected," not "organized" or "ordered"
    or "arranged" in any way. There is no notion of one variable
    being "before" or "after" or "adjacent to" another; they're just
    somewhere in the collection -- so you can forget about any notion
    you might have had about "mapping" a bunch of raw bytes into a
    Java object instance.

    The same applies even to the individual data items themselves:
    You know that a Java `int' is 32 bits wide, and that a Java `byte'
    is eight bits wide. It does *not* follow that an `int' consists of
    four `byte's -- that's true in C (often), but not in Java. As far
    as Java is concerned, the `int' is an indivisible 32-bit atomic
    value; it is *not* four individual `byte's, and most definitely not
    an array of four `byte's.

    Here's another thing: In C, an array is just a bunch of memory
    slots arranged contiguously. In Java, an array is a full-fledged
    object -- that is, Object -- with behaviors of its own. Use a bad
    index on a C array and there's no telling what might happen; do so
    in Java and you get an exception thrown into your teeth, reliably.
    Similar remarks apply to Strings versus strings: A string is just
    an array with a conventional structure, but a String is an object
    with methods and data and so on. A String is *not* an array of
    `char' -- try "Hello"[0] in Java, and I guarantee you will not
    get an 'H'.

    > All the C/C++ compilers we use have pragmas to force known
    > alignment/padding etc.


    That's unfortunate. It's probably tempted you to write bad
    C code, *slow* C code, in an attempt to duck the issues. Your
    compilers offer crutches; when you use them, you hobble like a
    cripple. You will never walk, much less run, until you visit
    Lourdes and discard your crutches there.

    > Byte order is not an issue in this layer,
    > since all parameters are encoded in strings.


    See the remarks above about the difference between strings
    and Strings. (Also, I doubt your assertion: Three of the four
    arrays you showed us are only one char long, and hence can hold
    only one C string value: "". It's possible, I guess, that your
    protocol involves transmitting multiple known-to-be-constant and
    hence information-empty bytes, but I doubt it. Most likely, you
    are not dealing with strings, not entirely at any rate.)

    --
    Eric Sosman
    lid
     
    Eric Sosman, Sep 30, 2009
    #14
  15. Mark wrote:
    > On Tue, 29 Sep 2009 11:33:29 +0200, Lothar Kimmeringer
    > <> wrote:
    >
    >> Mark wrote:
    >>
    >>> I am writing an app in java which reads data from a socket from a C
    >>> language program. The data is encoded as a C struct of char arrays
    >>> something like this;
    >>>
    >>> typedef struct {
    >>> char type[1];
    >>> char length[6];
    >>> char acknowledge[1];
    >>> char duplicate[1];
    >>> ...
    >>> } type_t;
    >>>
    >>> How can I decode a structure like this in Java without using JNI (a
    >>> requirement)?

    [...]
    > The data arrives to my code in already in a byte array.
    >
    > I guess I want to know if there is an easy way to map the C structure
    > to a java class or whether I will have to dissect the byte array
    > completely manually.


    Someone please put me out of my misery, I'm missing something obvious,
    why can't Mark do something like

    -------------------------------------8<-----------------------------------
    import java.util.Arrays;

    public class DecodeStruct {
    public static void main(String[] args) {

    char[] struct = {'t','0','0','0','0','2','1','N','A'};

    Foo foo = new Foo(struct);

    System.out.println("length is " + foo.getLengthAsString());
    }
    }

    class Foo {
    private char type;
    private char[] length;
    private char duplicate;
    private char acknowledge;

    Foo (char[] struct) {
    type = struct[0];
    length = Arrays.copyOfRange(struct, 1, 7);
    duplicate = struct[7];
    acknowledge = struct[8];
    }

    public String getLengthAsString() {
    return new String(length);
    }
    }
    -------------------------------------8<-----------------------------------
    Output:
    length is 000021

    --
    RGB
     
    RedGrittyBrick, Sep 30, 2009
    #15
  16. Lothar Kimmeringer

    Arne Vajhøj Guest

    RedGrittyBrick wrote:
    >
    > Mark wrote:
    >> On Tue, 29 Sep 2009 11:33:29 +0200, Lothar Kimmeringer
    >> <> wrote:
    >>
    >>> Mark wrote:
    >>>
    >>>> I am writing an app in java which reads data from a socket from a C
    >>>> language program. The data is encoded as a C struct of char arrays
    >>>> something like this;
    >>>>
    >>>> typedef struct {
    >>>> char type[1];
    >>>> char length[6];
    >>>> char acknowledge[1];
    >>>> char duplicate[1];
    >>>> ...
    >>>> } type_t;
    >>>>
    >>>> How can I decode a structure like this in Java without using JNI (a
    >>>> requirement)?

    > [...]
    >> The data arrives to my code in already in a byte array.
    >>
    >> I guess I want to know if there is an easy way to map the C structure
    >> to a java class or whether I will have to dissect the byte array
    >> completely manually.

    >
    > Someone please put me out of my misery, I'm missing something obvious,
    > why can't Mark do something like
    >
    > -------------------------------------8<-----------------------------------
    > import java.util.Arrays;
    >
    > public class DecodeStruct {
    > public static void main(String[] args) {
    >
    > char[] struct = {'t','0','0','0','0','2','1','N','A'};
    >
    > Foo foo = new Foo(struct);
    >
    > System.out.println("length is " + foo.getLengthAsString());
    > }
    > }
    >
    > class Foo {
    > private char type;
    > private char[] length;
    > private char duplicate;
    > private char acknowledge;
    >
    > Foo (char[] struct) {
    > type = struct[0];
    > length = Arrays.copyOfRange(struct, 1, 7);
    > duplicate = struct[7];
    > acknowledge = struct[8];
    > }
    >
    > public String getLengthAsString() {
    > return new String(length);
    > }
    > }
    > -------------------------------------8<-----------------------------------
    > Output:
    > length is 000021



    Does it "map the C structure to a java class" ?

    Arne
     
    Arne Vajhøj, Sep 30, 2009
    #16
  17. On 09/30/2009 05:44 AM, RedGrittyBrick wrote:
    > class Foo {
    > private char type;
    > private char[] length;
    > private char duplicate;
    > private char acknowledge;


    If the OP was using char because he wanted individual bytes and not
    because he was using textual data, byte would be more appropriate here,
    especially when he tries to read in or write out data...

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Sep 30, 2009
    #17
  18. On Wed, 30 Sep 2009 09:08:54 +0100, Mark wrote:

    > On 29 Sep 2009 15:10:36 GMT, "Kenneth P. Turvey" <>
    > wrote:
    >

    ..../....

    >>How does it arrive in your code? Is this the result of a native call?

    >
    > No. There must be no native code. In fact the project is a port from
    > native code.
    >
    > The data arrives from another Java package which handles the network
    > protocol.
    >

    In that case why are you talking about a C struct? Just swipe the code
    from the Java class that replaced the C struct.


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
     
    Martin Gregorie, Sep 30, 2009
    #18
  19. Lothar Kimmeringer

    markspace Guest

    Mark wrote:

    > On Tue, 29 Sep 2009 11:27:06 -0400, Eric Sosman <>


    >> What you "have" to do is decide what kind of a Java object (or
    >> set of objects) you want to create from this bag of bytes. Then do
    >> whatever's needed to create the object(s). Stop thinking about
    >> representation, and start thinking about information, about values.



    > I should have phrased my question better. I know this, I just need to
    > know how to implement it in Java.



    Well, can you tell us then? What kind of Java object do you want to
    create? Also tell us what you have now (an array of bytes, I'm
    guessing) and we might be able to give you some better advice.


    > My main problem is not knowing the Java class library very well.



    I don't think there's a library class that will do this for you. Maybe
    serialization, if you wrote special methods to handle it. Dunno if
    that's the best idea though.
     
    markspace, Sep 30, 2009
    #19
  20. Lothar Kimmeringer

    Lew Guest

    On Sep 30, 11:46 am, Mark <>
    wrote:
    > On Wed, 30 Sep 2009 12:36:34 +0000 (UTC), Martin Gregorie
    >
    >
    >
    > <> wrote:
    > >On Wed, 30 Sep 2009 09:08:54 +0100, Mark wrote:

    >
    > >> On 29 Sep 2009 15:10:36 GMT, "Kenneth P. Turvey" <>
    > >> wrote:

    >
    > >.../....

    >
    > >>>How does it arrive in your code?  Is this the result of a native call?

    >
    > >> No.  There must be no native code.  In fact the project is a port from
    > >> native code.

    >
    > >> The data arrives from another Java package which handles the network
    > >> protocol.

    >
    > >In that case why are you talking about a C struct? Just swipe the code
    > >from the Java class that replaced the C struct.

    >
    > The message format is described in terms of a C structure.  There is
    > no extant code to "replace" the C struct.  


    This contradicts what you said upthread:
    > The data arrives from another Java package which handles the network
    > protocol.
    >


    --
    Lew
     
    Lew, Sep 30, 2009
    #20
    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:
    408
    RA Scheltema
    Jan 6, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,393
    Chris Fogelklou
    Apr 20, 2004
  3. Roedy Green

    Re: Reading a C struct in java

    Roedy Green, Sep 29, 2009, in forum: Java
    Replies:
    0
    Views:
    397
    Roedy Green
    Sep 29, 2009
  4. Jeff Higgins

    Re: Reading a C struct in java

    Jeff Higgins, Oct 15, 2009, in forum: Java
    Replies:
    0
    Views:
    603
    Jeff Higgins
    Oct 15, 2009
  5. Ghislain Tanguay

    Reading .Net Web service user defined struct from a Java client

    Ghislain Tanguay, Aug 5, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    140
    Ghislain Tanguay
    Aug 5, 2004
Loading...

Share This Page