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
  • 9,232
  • 19
  • 74
  • 114

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
  • 90,369
  • 161
  • 124
  • 175
Adrian Leonhard
  • 6,660
  • 2
  • 24
  • 38