Share via


None Printable Character in C#

Question

Friday, May 8, 2020 9:48 PM

Please Tell me about none printable character .

What is Printable Character ?

how to print none Printable Character in C# Console Application. 

for Example ENQ , ETB ACK STX ETX , EOT ETC...

            string ENQ = char.ConvertFromUtf32(5); //Enquiry Character
            string ETB = char.ConvertFromUtf32(23); //End of trans block , 
            string ACK = char.ConvertFromUtf32(6); // Acknowledge
            string STX = char.ConvertFromUtf32(2); // Start of Text
            string ETX = char.ConvertFromUtf32(3); // END of Text
            string EOT = char.ConvertFromUtf32(4); // END of Transmittion
            string CR = char.ConvertFromUtf32(13); // Carriage return
            string LF = char.ConvertFromUtf32(13); // New Line , 
 

For Exaple ACK  = 6  , ENQ = 5 

"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL", "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI", "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US"

All replies (3)

Saturday, May 9, 2020 12:26 AM

Perhaps the easiest way to print on WinForm is to set the font to "Arial Unicode MS", and then add "0x2400" to character value if it is below 0x22 in order to set those characters in "Control Pictures and OCR" range of Unicode

Remember to also Console.OutputEncoding before printing.

I've compiled an example for you that will display "NUL" character. You may try to extend it for your purpose.

======

If you can ship your application with additional font, check out the GNU Unifont. seems it can enable you to print the control characters directly.

Just make sure you turn off "BEL" character processing because that is annoying.


Saturday, May 9, 2020 12:41 AM

how to print none Printable Character in C# Console Application. 

for Example ENQ , ETB ACK STX ETX , EOT ETC...

            string ENQ = char.ConvertFromUtf32(5); //Enquiry Character
            string ETB = char.ConvertFromUtf32(23); //End of trans block , 
            string ACK = char.ConvertFromUtf32(6); // Acknowledge
            string STX = char.ConvertFromUtf32(2); // Start of Text
            string ETX = char.ConvertFromUtf32(3); // END of Text
            string EOT = char.ConvertFromUtf32(4); // END of Transmittion
            string CR = char.ConvertFromUtf32(13); // Carriage return
            string LF = char.ConvertFromUtf32(13); // New Line , 
 

Obviously you can't print non-printable characters. That's why they're called 
non-printable.

To print a string representation of the control character you will have to do
the substitution yourself when and as needed. For example:

static void Main(string[] args)
{
    Console.WriteLine(ControlChars(4));
    Console.ReadLine();
}

static string ControlChars(int cc)
{
    switch (cc)
    {
        case 2:
            return "<STX>";
        case 3:
            return "<ETX>";
        case 4:
            return "<EOT>";
        case 5:
            return "<ENQ>";
        case 6:
            return "<ACK>";

        default:
            return "";
    }
}

Note also that in your posted code you have:

string CR = char.ConvertFromUtf32(13); // Carriage return
string LF = char.ConvertFromUtf32(13); // New Line , 

A linefeed is decimal 10 (0x0A), not 13.

  • Wayne

Monday, May 11, 2020 6:00 AM

Hi saqsaqPK,
>>Tell me about none printable character. what is Printable Character?
Non-printable characters are parts of a character set, they are in the context of signal and control in character encoding.
Non-printable characters are used to indicate certain formatting actions, such as: White spaces, Carriage returns, Tabs, Line breaks, Page breaks, Null characters.
A printable character is a character that occupies a printing position on a display. For the standard ASCII character set, printing characters are all with an ASCII code greater than 0x1f (US), except 0x7f (DEL).
***>>how to print none Printable Character in C# Console Application. ***
As WayneAKing said, you can't print non-printable characters. That's why they're called non-printable. And he provides a code example to explain.
And what I want to add is that you can use char.IsControl() method to test whether a character is a control character(non-printable) or not:

foreach (var c in str)
{
    if (char.IsControl(c))
    {
        Console.WriteLine("Found control character: {0}", (int)c);
    }
}

Best Regards,
Daniel Zhang

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].