P
P
postflow2016-08-02 10:16:27
Android
postflow, 2016-08-02 10:16:27

How to pass two parameters of different type to a method or how to cast int to byte?

Two variables of type Int and byte[]

int frame_len = init_frame().toByteArray().length;
byte [] frame_to_byte_array = init_frame().toByteArray();

the method accepts the byte type as input:
...
    public void write(byte[]  message) {
            try {
                mmOutStream.write(message);
            } catch (IOException e) {
            }
        }
...

to the write(byte[] message) method, you need to pass "frame_len" first, then the "frame_to_byte_array" data itself,
but in one action ..
On phyton-e, it works like this:
...
sock.write(chr(len(data)) + data)
...

How to pass two different type parameters to a method in Java?
mConnectedThread.write(frame_len + frame_to_byte_array);

Or How to cast an int to a byte . and

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Алексей Перелыгин, 2016-08-02
@orcDamnar

Я бы предложил сделать так:

public void write(byte[]  message) {
            try {
                mmOutStream.write(message.length);
                mmOutStream.write(message);
            } catch (IOException e) {
            }
        }

C
coden55, 2016-08-02
@coden55

byte[] frame_len = ByteBuffer.allocate(4).putInt(frame_to_byte_array.length).array();
int aLen = frame_len.length;
int bLen = frame_to_byte_array.length;
byte[] result = new byte[aLen+bLen];
System.arraycopy(frame_len, 0, result, 0, aLen);
System.arraycopy(frame_to_byte_array, 0, result, aLen, bLen);
   
mConnectedThread.write(result);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question