473,666 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

return the printable form of a character

i want to have a function which can print the printable form (possibly a 2
character string) of a character on UNIX like systems. for example, if i were to
pass the ascii value '\3', i would like it printed "^C".

there is a file called charset.c in the distribution of less
[ftp://ftp.gnu.org/gnu/less/less-382.tar.gz] which achieves this, but it is
quite long winded and i do not wish to have to copy all the relevant code, as it
is longer than my program which requires this functionality.

i was wondering if anyone knew of a much smaller implementation. .. preferably
already in a GNU library?
Nov 14 '05 #1
8 1959
In 'comp.lang.c', Sam Halliday <em***@example. com> wrote:
i want to have a function which can print the printable form (possibly a
2 character string) of a character on UNIX like systems. for example, if
i were to pass the ascii value '\3', i would like it printed "^C".


The idiomatic response to your question is:

"Do it yourself and post your ISO-C code if you are stuck."

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #2
Sam Halliday wrote:
i want to have a function which can print the printable form (possibly a 2
character string) of a character on UNIX like systems. for example, if i were to
pass the ascii value '\3', i would like it printed "^C".

there is a file called charset.c in the distribution of less
[ftp://ftp.gnu.org/gnu/less/less-382.tar.gz] which achieves this, but it is
quite long winded and i do not wish to have to copy all the relevant code, as it
is longer than my program which requires this functionality.

i was wondering if anyone knew of a much smaller implementation. .. preferably
already in a GNU library?


Using the ASCII table, there are two sets of nonprintable
characters: 0 .. 0x1F and 0x7F. Let us ignore the last one.
The set 0x00 .. 0x1F is known as control characters. Each one
has been related to pressing the CTRL key and a character,
such as 0x04 == CTRL-D. The "CTRL-" part is often replaced
with the '^' [caret] character.

So, if the value is less than 27, use ^ and a capital letter.
I believe ^@ represents 0x00. Try adding 0x40 to the value
to obtain the character part:
printf("^%c", value + 0x40);
or
printf("%c%c", '^', value + 0x40);
Note that this only applies to the ASCII chart. All other
encodings may not work.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #3
On Mon, 12 Jul 2004, Sam Halliday wrote:
i want to have a function which can print the printable form (possibly a
2 character string) of a character on UNIX like systems. for example, if
i were to pass the ascii value '\3', i would like it printed "^C".

there is a file called charset.c in the distribution of less
[ftp://ftp.gnu.org/gnu/less/less-382.tar.gz] which achieves this, but it
is quite long winded and i do not wish to have to copy all the relevant
code, as it is longer than my program which requires this functionality.
Roll your own. Have a look at the isprint function. If isprint is true
then just print it. If not then set a rule for converting it. For example,
check is c+'A' is printable. If yes, print '^' then c+'A'.

If you are having trouble with the code post it here.
i was wondering if anyone knew of a much smaller implementation. .. preferably
already in a GNU library?


Not I.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@ whitehouse.gov
Nov 14 '05 #4
Emmanuel Delahaye wrote:
In 'comp.lang.c', Sam Halliday <em***@example. com> wrote:
i want to have a function which can print the printable form (possibly a
2 character string) of a character on UNIX like systems. for example, if
i were to pass the ascii value '\3', i would like it printed "^C".


The idiomatic response to your question is:

"Do it yourself and post your ISO-C code if you are stuck."


for what its worth... heres what i ended up doing (in the context of an
example). it only works for iso-8859 character maps, but thats all i need it
for. this is handy code to check and see what graphs are in your current console
fonts.

#include <stdio.h>
#include <stdlib.h>

/* http://en.wikipedia.org/wiki/Control_character */
char asciicntrl[][4]={
"NUL","SOH","ST X","ETX","EOT", "ENQ","ACK","BE L","BS","HT" ,
"LF","VT","FF", "CR","SO","SI", "DLE","DC1","DC 2","DC3","DC 4",
"NAK","SYN","ET B","CAN","EM"," SUB","ESC","FS" ,"GS","RS","US" };
char isocntrl[][5]={
"PAD","HOP","BP H","NBH","IND", "NEL","SSA","ES A","HTS","HT J",
"VTS","PLD","PL U","RI","SS2"," SS3","DCS","PU1 ","PU2","ST S",
"CCH","MW","SPA ","EPA","SOS"," SGCI","SCI","CS I","ST","OSC"," PM","APC"};

void printablechar(c har c);

main()
{
char i,n;

for (i=0,n=1;i<255; i++,n++){
printf("%d\t",i );
printablechar(i );
printf("\t");
if(7==n){
printf("\n");
n=0;
}
}
printf("\n");

return 0;
}

void printablechar(c har c)
{
/* ASCII control sequences */
if (127==c)
printf("DEL");
else if (32>c)
printf("%s",asc iicntrl[c]);

/* ISO-8859 control sequences */
else if ((127<c)&&(160> c))
printf("%s",iso cntrl[c-128]);

/* printable characters */
else
printf("%c",c);
}
Nov 14 '05 #5
Sam Halliday wrote:
for what its worth... heres what i ended up doing (in the context of an
example). it only works for iso-8859 character maps, but thats all i need it
for. this is handy code to check and see what graphs are in your current
console fonts.


well... seems i broke x86 with that. for completeness here is the version you
should use, if you are interested:

#include <stdio.h>
#include <stdlib.h>

/* http://en.wikipedia.org/wiki/Control_character */
char asciicntrl[32][4] =
{ "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"
};
char isocntrl[32][5] =
{ "PAD", "HOP", "BPH", "NBH", "IND", "NEL", "SSA", "ESA", "HTS", "HTJ",
"VTS", "PLD", "PLU", "RI", "SS2", "SS3", "DCS", "PU1", "PU2", "STS",
"CCH", "MW", "SPA", "EPA", "SOS", "SGCI", "SCI", "CSI", "ST", "OSC", "PM",
"APC"
};

void printablechar (unsigned char c);

int
main ()
{
int i, n;

for (i = 0, n = 1; i < 256; i++, n++)
{
printf ("%d\t", (unsigned char) i);
printablechar ((unsigned char) i);
printf ("\t");
if (7 == n)
{
printf ("\n");
n = 0;
}
}
printf ("\n");

return 0;
}
void
printablechar (unsigned char c)
{
/* ASCII control sequences */
if (127 == c)
printf ("DEL");
else if (32 > c)
printf ("%s", asciicntrl[(int) c]);

/* ISO-8859 control sequences */
else if ((127 < c) && (160 > c))
printf ("%s", isocntrl[(int) (c - 128)]);

/* printable characters */
else
printf ("%c", c);
}
Nov 14 '05 #6
In 'comp.lang.c', Sam Halliday <em***@example. com> wrote:
Sam Halliday wrote:
for what its worth... heres what i ended up doing (in the context of an
Come on, be proud of yourself. You've done the job.
example). it only works for iso-8859 character maps, but thats all i
need it for. this is handy code to check and see what graphs are in
your current console fonts.


printf ("%d\t", (unsigned char) i);


"%d" expects an int. The cast is useless.

Sounds to be correct but not portable C, and to do the job. What was your
question again ?

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #7
Emmanuel Delahaye wrote:
printf ("%d\t", (unsigned char) i); "%d" expects an int. The cast is useless.


yeah, wasn't thinkin right
Sounds to be correct but not portable C, and to do the job. What was your
question again ?


i basically wanted to know if a (standard) function existed that does what my
printablechar() does. mine is limited to iso-8859 charsets, a standard function
would supposedly support all charsets.
Nov 14 '05 #8
In 'comp.lang.c', Sam Halliday <em***@example. com> wrote:
Emmanuel Delahaye wrote:
> printf ("%d\t", (unsigned char) i);

"%d" expects an int. The cast is useless.


yeah, wasn't thinkin right
Sounds to be correct but not portable C, and to do the job. What was
your question again ?


i basically wanted to know if a (standard) function existed that does
what my printablechar() does. mine is limited to iso-8859 charsets, a
standard function would supposedly support all charsets.


No. There is not such a function. If there was one, I guess it would
only support the charset of the implementation.

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4877
by: farseer | last post by:
Hi, i have a file which contains data that is delimted by non-printable character "0x1f". how can i parse this file with that character as the delimiter?
3
1339
by: Rod | last post by:
I want to produce some reports designed for printing. (like you can in Access). I can't find the equivalent, what's the accepted way of achieving something similar. many thanks Rod
1
5160
by: alexttp | last post by:
I am trying to make sure that - when saving some XML - only the printable characters are saved "as is" - and all the rest would be converted into their "&#x{0};" representation. Years ago ;-) there was an IsPrintable() function, but it is not present in C# Char class implementation. So for this purpose I check the character with: 1. Char.IsSurrogate() - to check if the character is one of multibyte letter bytes (such bytes will be...
6
4161
by: Keith Smith | last post by:
How can I say "If e is equal to a printable character (as opposed to Insert, Del, Home, PageUp, PageDown, etc) then x=1"? Something like... if (e.KeyChar==PrintableCharacters) { x=1; }
5
3278
by: **Developer** | last post by:
How do I know if the value in a KeyEventArgs is printable? Basicacally, in KeyUp I'd like to know if the character is printable, as opposed to say, a backspace. I suppose I could check KeyValue against some integers which I could get from an ASCII table. Isn't there a better way?
10
2031
by: Chad | last post by:
Given: #include <stdio.h> #include <stdlib.h> #define MAXLINE 200 int main(void) { char buff;
4
2513
by: John K Masters | last post by:
>From what I have read the string module is obsolete and should not be used but I am working on a project that parses printable files created in a DOS program and creates a web page for each file. I am using the string.printable constant to determine which characters should be kept; the files contain many print control codes. There seems to be nothing like this in the string methods. isalnum() seems the nearest but gives false for '+' '!'...
33
2971
by: Pietro Cerutti | last post by:
Hi group, assume the following declarations: char *func_1(void); void func_2(char **); I am allowed to do: char *c = func_1();
3
1994
by: user1357 | last post by:
Hi All, i am trying to write a code which will take a non printable character as an input and used as a separator for different fields ex : if user gives an input \xd1 the o/p should be field1 ╤ field2 initially i have written a code to get the below lines in my program hex.pl
0
8348
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8863
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7376
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4356
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1761
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.