Why are these considered "Non-Printable ASCII Characters"?
binary - How to find non-printable character in text file - Stack Overflow
python - Show non printable characters in a string - Stack Overflow
How can I insert non-printing special characters in component and Tag/Layer names?
What is a non-printing character?
Non-printing characters, essential in computing, programming, and communications, are symbols that don't visibly appear when printed or displayed. Examples include newline ( ) for line breaks and tab ( ) for spacing. They play a vital role in text formatting, coding, and data transmission. Understanding and correctly utilizing these characters ensures well-structured code, readable text, and effective communication. Proper handling of non-printing characters is crucial in preventing formatting issues, maintaining data integrity, and facilitating seamless interactions within the digital realm.
Why are non-printing characters important in computing?
Non-printing characters are crucial in computing for structuring and organizing text, enhancing code readability, and ensuring proper data transmission. These invisible characters, such as newline and tab, act as formatting tools, facilitating clean code presentation. In programming, they play a pivotal role in code structure, making it more readable and maintainable. Additionally, in data transmission, non-printing characters, like Start of Text (STX) and End of Text (ETX), define the beginning and end of data, ensuring accurate communication.
How do non-printing characters impact programming?
In programming, non-printing characters are the unsung heroes. They enable clear code formatting, making it readable and organized. For instance, indentation, achieved with tab characters, enhances code readability, and newline characters structure the code into manageable sections.
I have created a spreadsheet that has some ASCII characters in some cells but my ASCII checker is saying everything is non-printable ASCII characters.
I have provided a link to my file. I have highlighted the cells that have non-printable ASCII characters.
Non-Printable ASCII File
Any help figuring out what I am doing wrong would be appreciated. Thanks.
I don't know of any built-in method, but it's fairly easy to do using a comprehension:
import string
printable = string.ascii_letters + string.digits + string.punctuation + ' '
def hex_escape(s):
return ''.join(c if c in printable else r'\x{0:02x}'.format(ord(c)) for c in s)
I'm kind of late to the party, but if you need it for simple debugging, I found that this works:
string = "\n\t\nHELLO\n\t\n\a\17"
procd = [c for c in string]
print(procd)
# Prints ['\n,', '\t,', '\n,', 'H,', 'E,', 'L,', 'L,', 'O,', '\n,', '\t,', '\n,', '\x07,', '\x0f,']
While just list is simpler, a comprehension makes it easier to add in filtering/mapping if necessary.
You could do the comparison of the bytes by storing the strings as constants, with decent names. So case "\0\0": would become case STATUS_CRYPTO_1: which helps remove the 'magic number' aspect of your switch statement.
But the correct solution here is to add some comments. That's what comments were designed for, that's their intended purpose. Some documentation at the top of your function is probably the best place to say what the byte array is that you're comparing against, and why.
As can already be seen in the question, using such non-printable characters directly in a string-literal makes the code essentially unreadable. At first glance, you would say that the presented code should fail to compile because there are multiple identical case labels.
Most languages (and that includes C#) support escape sequences to deal with troublesome characters like line-breaks, quotes and non-printable characters. Just like that you would use \n to represent a line-break in a string, you can use \x01 to represent the character U+0001 (ASCII SOH)in a string.
This is very commonly used when the string type is (mis-)used to represent byte data. As an example, your code would become:
switch (s)
{
case "\x00\x00":
Console.WriteLine("Crypto method 00");
break;
case "\x00\x01":
Console.WriteLine("Crypto method 01");
break;
case "\x00\x02":
Console.WriteLine("Crypto method 02");
break;
case "\x00\x03":
Console.WriteLine("Crypto method 03");
break;
default:
Console.WriteLine(s);
break;
}
The graphic characters would be the one for which the isgraph()/iswgraph() standard functions return true or the ones matched by the [[:graph:]] regular expressions, that is the ones in the graph character class in the locale.
Per POSIX, the print class must be a superset of graph and be disjunct from cntrl and graph must be a superset of upper, lower, alpha, digit, xdigit, and punct and must not include the space (U+0020) character (with no mention of other whitespace characters).
The idea being that the graphic characters would be the ones for which ink would be used to draw them, while printable would be the non-control ones.
In practice, on GNU systems (such as Ubuntu) at least print is graph plus the non-control characters from the space class. Here with glibc 2.35 (as used on Ubuntu 22.04) and in UTF-8 locales, that includes:
U+0020 SPACE
U+1680 OGHAM SPACE MARK
U+2000 EN QUAD
U+2001 EM QUAD
U+2002 EN SPACE
U+2003 EM SPACE
U+2004 THREE-PER-EM SPACE
U+2005 FOUR-PER-EM SPACE
U+2006 SIX-PER-EM SPACE
U+2008 PUNCTUATION SPACE
U+2009 THIN SPACE
U+200A HAIR SPACE
U+205F MEDIUM MATHEMATICAL SPACE
U+3000 IDEOGRAPHIC SPACE
While the space class has:
U+0009 CHARACTER TABULATION
U+000A LINE FEED
U+000B LINE TABULATION
U+000C FORM FEED
U+000D CARRIAGE RETURN
U+0020 SPACE
U+1680 OGHAM SPACE MARK
U+2000 EN QUAD
U+2001 EM QUAD
U+2002 EN SPACE
U+2003 EM SPACE
U+2004 THREE-PER-EM SPACE
U+2005 FOUR-PER-EM SPACE
U+2006 SIX-PER-EM SPACE
U+2008 PUNCTUATION SPACE
U+2009 THIN SPACE
U+200A HAIR SPACE
U+2028 LINE SEPARATOR
U+2029 PARAGRAPH SEPARATOR
U+205F MEDIUM MATHEMATICAL SPACE
U+3000 IDEOGRAPHIC SPACE
This Bash script tabulates the character classes associated with each character in the ASCII set (according to GNU/awk definitions).
#! /bin/bash --
Awk='
BEGIN {
Ctl1 = "SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI ";
Ctl2 = "DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US SPACE";
split (Ctl1 Ctl2, Ctl); Ctl[0] = "NUL"; Ctl[127] = "DEL";
C = "cntrl print graph space blank punct alnum alpha digit lower upper xdigit";
split (C, Class);
}
function Char (n, ch, Local, j) {
printf ("0x%.2X %5s", n, (n <= 32 || n == 127) ? Ctl[n] : ch);
for (j = 1; j in Class; ++j)
if (ch ~ "[[:" Class[j] ":]]") printf (" :%s:", Class[j]);
printf ("\n");
}
{ for (j = 0; j < 128; j++) Char( j, sprintf ("%c", j)); }
'
echo | awk -f <( printf '%s' "${Awk}" )