问题
Is there a way to get the name of the value of ascii.
for example
0x08 or just 8 is the backspace
can I get the name "Backspace" in c or c++?
回答1:
In short, no. Easily worked around, though.
If your ASCII code is < 32, here's table of standard ASCII control character abbreviations you can use:
char *ascii_cc[] = {
"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" };
Then just index into that array of strings by the value of the char you're interested in. e.g.
char c = 8; /* backspace */
printf("ASCII control code = %s\n", ascii_cc[c]);
回答2:
There is no way to do that in the standard and I am not aware of a third-party library capable to do that. Your best option is to create a table with the mappings on your own. You will only have to create a few special mappings as most of the characters are called the way they are displayed.
回答3:
There are only 128 of them (the last 128 differ, depending on what code page do you use), why don't you create a const array? If you narrow down your data to non-printable characters, the number of items drops to 32.
You can find the complete list on the Wikipedia.
Here are first 32 values:
Dec Abbr Name 0 NUL Null character 1 SOH Start of Header 2 STX Start of Text 3 ETX End of Text 4 EOT End of Transmission 5 ENQ Enquiry 6 ACK Acknowledgment 7 BEL Bell 8 BS Backspace[d][e] 9 HT Horizontal Tab[f] 10 LF Line feed 11 VT Vertical Tab 12 FF Form feed 13 CR Carriage return[g] 14 SO Shift Out 15 SI Shift In 16 DLE Data Link Escape 17 DC1 Device Control 1 (oft. XON) 18 DC2 Device Control 2 19 DC3 Device Control 3 (oft. XOFF) 20 DC4 Device Control 4 21 NAK Negative Acknowledgement 22 SYN Synchronous idle 23 ETB End of Transmission Block 24 CAN Cancel 25 EM End of Medium 26 SUB Substitute 27 ESC Escape[i] 28 FS File Separator 29 GS Group Separator 30 RS Record Separator 31 US Unit Separator
回答4:
man ASCII can help:
#include <stdio.h>
#include <string.h>
FILE *popen(const char *command, const char *mode);
int pclose(FILE *stream);
int main(void)
{
FILE *cmd = popen("man ASCII", "r");
char *key = "010";
char result[128];
while (fgets(result, sizeof(result), cmd) != NULL)
if (strstr(result, key))
printf("key %s: %s", key, result);
pclose(cmd);
return 0;
}
来源:https://stackoverflow.com/questions/19785619/ascii-name-of-value