473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 1937
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.learn.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","STX","ETX","EOT","ENQ","ACK","BEL","B S","HT",
"LF","VT","FF","CR","SO","SI","DLE","DC1","DC2","D C3","DC4",
"NAK","SYN","ETB","CAN","EM","SUB","ESC","FS","GS" ,"RS","US"};
char isocntrl[][5]={
"PAD","HOP","BPH","NBH","IND","NEL","SSA","ESA","H TS","HTJ",
"VTS","PLD","PLU","RI","SS2","SS3","DCS","PU1","PU 2","STS",
"CCH","MW","SPA","EPA","SOS","SGCI","SCI","CSI","S T","OSC","PM","APC"};

void printablechar(char 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(char c)
{
/* ASCII control sequences */
if (127==c)
printf("DEL");
else if (32>c)
printf("%s",asciicntrl[c]);

/* ISO-8859 control sequences */
else if ((127<c)&&(160>c))
printf("%s",isocntrl[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
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
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
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 ;-)...
6
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
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...
10
by: Chad | last post by:
Given: #include <stdio.h> #include <stdlib.h> #define MAXLINE 200 int main(void) { char buff;
4
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....
33
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
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 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.