简体   繁体   中英

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?

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 \ . 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, eg \\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 \ through \ÿ , so Unicode escapes are usually preferred.

Here, <CR> means "carriage-return", ie "\\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 , eg \ for STX.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM