Mark wrote:
On Tue, 29 Sep 2009 11:33:29 +0200, Lothar Kimmeringer
<news200709@kimmeringer.de> 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
Yes. Something like this.
(\__/) M.
(")_(") 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.