S
S
ser-mk2014-06-26 17:27:43
Java
ser-mk, 2014-06-26 17:27:43

What is the most convenient way to emit a C (C ++) structure in java (under Android)?

Hello!
Data is transmitted to the android program via Bluetooth. Data represents large structures, which in C are described like this

struct
{
uint16_t  data0;
uint32_t  data1;
uint16_t  data2;
.....

uint8_t dataN
}

structure elements can be of arbitrary length up to (4 bytes). The structure is packed, without alignment.
My technique is made in C++ NDK, where I store them and access them using a separate function, which is a banal switch.
I consider this approach to be a dead end. I would like to copy structures in java and use them there just like in Xia. I know Java does not support direct memory manipulation, but I hope there is a workaround.
I deal with java and android for the first time. Prior to that, he worked with C ++ on MK.
What will be the suggestions how to simulate the work of the structure in java?
PS You can't refuse structures (

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
vlad20012, 2014-06-26
@ser-mk

In Java, it is common practice to read/write each variable separately from/to a DataInputStream/DataOutputStream. To read/write the whole structure, we create the methods writeTo(DataOutoutStream data), readFrom(DataInputStream data) and write all elements of the structure there, for example

public class SomeStruct {
    public int i1;
    public int i2;

    public void writeTo(DataOutputStream data) {
        data.writeInt(i1);
        data.writeInt(i2);
    }
    public void readFrom(DataInputStream data) {
        i1 = data.readInt();
        i2 = data.readInt();
    }
}

But due to the fact that the record is made in the C code, problems may arise. The thing is, there are two options for byte order . Which one you get depends on the platform. On x86, little-endian is used. But when reading from the DataInputStream, big endian is used. And in general, when transmitting over a network, it is customary to use big endian. So when writing, you will have to expand the bytes. But if it is very pripret - it is possible when reading. For example, the byte order of type int can be changed like thisInteger.reverseBytes(i1);

M
Marat S, 2014-06-26
@aratj

it is not clear what you want to see on the android side, how you want to work with it in the future.
and what's the catch specifically?

L
lo-fi, 2014-06-27
@hrls

Perhaps you should abandon the attempt to emulate structures with access by a named field and pay attention to the java.util.BitSet class

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question