0

I need to get the bytes of the control character STX (02h) to append to a stream I need to send to a controller board.

How do I get the bytes of the STX control character in Java?

Roy Hinkley
  • 10,111
  • 21
  • 80
  • 120

1 Answers1

2

The bytes of the control character depend on the encoding the controller board uses. For ASCII:

"\u0002".getBytes(StandardCharsets.US_ASCII);

Note that if you are writing characters to an output stream, you should wrap it with a Writer which is configured with the correct encoding:

OutputStream controllerOutputStream;
Charset controllerCharset = StandardCharsets.US_ASCII; // probably ASCII
Writer controllerWriter = new OutputStreamWriter(controllerOutputStream, controllerCharset);

controllerWriter.append('\u0002'); // append a single character
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Adrian Leonhard
  • 7,040
  • 2
  • 24
  • 38