c control character question

c control character question

Post by mj » Wed, 20 Jun 2001 22:49:15



Hi,

I was searching for a way to capture Ctrl-<char> keys, and stumbled
across a macro definition like this:


used as follows:

if((c = getchar()) == ctrl('G')){
        /* .. handle Ctrl-G .. */

Quote:}


Is this a standard/legit way to capture Ctrl-<something> characters?

Thanks in advance,
                        -mikko

 
 
 

c control character question

Post by John Gord » Wed, 20 Jun 2001 23:27:28



> I was searching for a way to capture Ctrl-<char> keys, and stumbled
> across a macro definition like this:

> used as follows:
> if((c = getchar()) == ctrl('G')){
>         /* .. handle Ctrl-G .. */
> }



in ASCII, the control characters have values of 1-26, and the uppercase
alphabet characters have values of 65-90.  therefore, if you subtract
64 from the ASCII code of an uppercase character, you will get the

have an ASCII value of 64.

Quote:> Is this a standard/legit way to capture Ctrl-<something> characters?

i'm not sure if there is really a "standard" way, but it looks fine to me.

(strictly speaking, the actual capture is done by getchar.  the ctrl macro
is used for _evaluating_ the captured value.)

---
"... What with you being his parents and all, I think that you could
be trusted not to shaft him."  -- Robert Chang, rec.games.board



 
 
 

c control character question

Post by Eric Sosma » Wed, 20 Jun 2001 23:56:26




> > I was searching for a way to capture Ctrl-<char> keys, and stumbled
> > across a macro definition like this:


> > used as follows:

> > if((c = getchar()) == ctrl('G')){
> >         /* .. handle Ctrl-G .. */
> > }



> in ASCII, the control characters have values of 1-26, and the uppercase
> alphabet characters have values of 65-90.  therefore, if you subtract
> 64 from the ASCII code of an uppercase character, you will get the

> have an ASCII value of 64.

    The subtraction yields the desired result only if the
given character is an ASCII upper-case letter.  All of the
following will give some sort of garbage:

        ctrl('g')
        ctrl('0')       /* looks like SO, but it isn't */
        ctrl('\n')

    One could argue that these are mis-uses of the macro;
the programmer has made a mistaik.  This is true, but the
compiler will accept all of them without protest, meaning
the error will be detected later (if at all) rather than
sooner.  Debugging sessions take time and effort; wouldn't
it be nicer if the compiler squawked right away when it
first encountered the error?

    For this and other reasons, I recommend less cleverness:

        #define CTRL_A '\001'
        #define CTRL_B '\002'
        ...

or

        #define SOH '\001'
        #define STX '\002'
        ...

or

        enum {
           NUL, SOH, STX, ... };

    With "exhaustive" listings like this, a programmer who
accidentally writes `if (ch == ctrl_g)' will get an error
message right away instead of a program malfunction later on.
Safer, I'd say, and much cheaper in the long run.

--

 
 
 

c control character question

Post by Lew Pitch » Thu, 21 Jun 2001 00:25:35






>> > I was searching for a way to capture Ctrl-<char> keys, and stumbled
>> > across a macro definition like this:


>> > used as follows:

>> > if((c = getchar()) == ctrl('G')){
>> >         /* .. handle Ctrl-G .. */
>> > }




[snip]

ASCII control characters can be properly derived from their printing
counterparts by the use of an AND mask, as in

  #define CTRL(c) ((c) & 0x1f)

This works for *both* uppercase and lowercase source characters. That
is,
    CTRL('G') == CTRL('G')

Lew Pitcher, Information Technology Consultant, Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employer's.)

 
 
 

c control character question

Post by Nithyanandha » Thu, 21 Jun 2001 00:32:29



> > I was searching for a way to capture Ctrl-<char> keys, and stumbled
> > across a macro definition like this:


> > used as follows:

> > if((c = getchar()) == ctrl('G')){
> >         /* .. handle Ctrl-G .. */
> > }



> in ASCII, the control characters have values of 1-26

No.  It is 0-31 and 127. The space character(blank, ascii value 32) can be put
under either graphic character or control character or both.
27 - ESC
28 - File seperator
29 - Group seperator.
30 - Record seperator.
31 - Unit seperator.
127 - delete

Look at http://www.itscj.ipsj.or.jp/ISO-IR/001.pdf to see the control characters
registered by ISO 646.

--
Nithyanand.
Siemens, Bangalore, India.
(Opinions expressed are my own and do not reflect the opinions of my employer,
Siemens)

 
 
 

c control character question

Post by Lew Pitch » Thu, 21 Jun 2001 00:28:52


Correction of a typo....







>>> > I was searching for a way to capture Ctrl-<char> keys, and stumbled
>>> > across a macro definition like this:


>>> > used as follows:

>>> > if((c = getchar()) == ctrl('G')){
>>> >         /* .. handle Ctrl-G .. */
>>> > }



>[snip]

>ASCII control characters can be properly derived from their printing
>counterparts by the use of an AND mask, as in

>  #define CTRL(c) ((c) & 0x1f)

>This works for *both* uppercase and lowercase source characters. That
>is,
>    CTRL('G') == CTRL('G')

Of course, I meant
     CTRL('G') == CTRL('g')

>Lew Pitcher, Information Technology Consultant, Toronto Dominion Bank Financial Group

>(Opinions expressed are my own, not my employer's.)

Lew Pitcher, Information Technology Consultant, Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employer's.)

 
 
 

c control character question

Post by Eric Sosma » Thu, 21 Jun 2001 00:11:27






> > [...]

>         ctrl('0')       /* looks like SO, but it isn't */

    "Upon further review," as they say, it really doesn't look
much like SO at all.  I seem to have mistpyed SI ...  Put it
down to silly programmer errors and the need to defend
against same ...

--

 
 
 

1. hex code of a control character question?

Does any guru out there know the code of the '\c' ?

I have a file that came from dos and every so often there is a '^M'
character.
I need to remove it and join the line with the next line in the file.
I was going to try and modify a dos2unix source to do what I want to
accomplish.
Unless some knows of a script that does that?

Thanks
Jeff

2. redhat and debian

3. Questions regarding recognizing control/escape characters.

4. position announcement

5. REMOVING CONTROL CHARACTERS

6. Upgrade XFree86

7. Telnet control character

8. HELP!! fflush(stdio) in linux dosen't seem to work!!

9. Control characters

10. add control characters using sed with escape sequences?

11. Print control characters

12. printing control Characters in addch(), echochar()

13. Control Characters and MS Word