The Well
The Well

Reputation: 884

How to write ASCII in the OutputStream

According to the Datamax Documentation:

<CR> is used to identify the line termination character. Other strings placed
between < > in this manual represent the character of the same ASCII name, and
are single-byte hexadecimal values (e.g., <STX>, <CR>, and <0x0D> equal 02, 0D,
and 0D, respectively). 

I was trying to write the code < CR> in the Printer OutputStream but I dont know how? I tried the following code:

outputStream.write("<CR>");

But it didnt worked. How do I write ASCII and Hexadecimal in the outputstream?

Upvotes: 1

Views: 1291

Answers (2)

Andreas
Andreas

Reputation: 159114

See the ASCII table: http://www.asciitable.com/

<CR> is hex D, decimal 13, octal 015.

In a Java string literal, this can be escaped in 3 ways:

Looking at that ASCII table again, you can also see that <STX> is hex 2, decimal 2, octal 2, which in Java is either \002 or \u0002. There is no letter version of <STX>, like the \r for <CR>.

Note: Octal form can be written in 1-, 2-, or 3-digit variants, e.g. \2, \02, \002, but 3-digit version is best. Actually, the JLS says:

Octal escapes are provided for compatibility with C, but can express only Unicode values \u0000 through \u00FF, so Unicode escapes are usually preferred.

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1500785

Here, <CR> means "carriage-return", i.e. "\r". However, if you've got an OutputStream and you want to write text to it, I would suggest wrapping it in an OutputStreamWriter, specifying the appropriate encoding. You can then use text-oriented calls appropriately.

For other values, you can use \uxxxx, e.g. \u0002 for STX.

Upvotes: 1

Related Questions