M
M
mr_blond972017-07-02 18:52:00
Java
mr_blond97, 2017-07-02 18:52:00

How to pass a byte through getOutputStream()?

There is a byte and a websocket connection.

I'm trying to send a byte in a message like this:

byte newByte = 11;
newByte = 11;
client.getOutputStream().write(newByte);


It doesn't work, although the string converted with .getBytes is passed: How to pass a byte through getOutputStream()?
client.getOutputStream().write(("0").getBytes());

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2017-07-02
@mr_blond97

That's right, OutputStream.write() takes an array of bytes, which String.getBytes() just returns. To pass one byte, you need to pass an array of one element to the write method:
Only the design remained incomprehensible to me . If you need to declare a variable containing one byte, then it should look like this:byte newByte = new newByte;
byte b = 11;

M
mr_blond97, 2017-07-03
@mr_blond97

There was a typo in the design, I need to transfer an array of bytes, but I didn’t succeed and I started trying to transfer bytes.
It fails to send:

byte newByte = 11;
client.getOutputStream().write(new byte[] {newByte});

This also fails:
byte[] newByte = new byte[1];
newByte[0] = 11;
client.getOutputStream().write(newByte);

Sometimes something can be transmitted, for example, when an unknown character "?" will be displayed in the console. At the same time, this code will work:newByte[0] = 0xffffffcf;
byte[] newByte = new byte[1];
newByte = ("11").getBytes();
client.getOutputStream().write(newByte);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question