my_question
my_question

Reputation: 3235

How to feed/read non printable character

Here is my code:

#include <string.h>
#include <stdio.h>

int main( int argc, char** argv)
{
        static char tmpBuf[ 4 ];
        long idx;
        unsigned int cks;
        int bufLen = strlen(argv[1]);

        for ( idx = 0, cks = 0; idx < bufLen; idx++) {
            unsigned char c = argv[1][idx];
            printf("Char=%c|", c);
            cks += c;
        }
        sprintf( tmpBuf, "%03d", (unsigned int)( cks % 256 ) );
        printf( "\n%s\n", tmpBuf ); 

        return 0;
}

Basically it sums up all the characters which may contain non printable ones. In my testing:

>./a.out  "\1"
Char=\|Char=1|
141

You can see that the program interprets "\1" as 2 separate characters instead of an escaped one char.

How can I make the code take escape to take non-printable char?

Upvotes: 0

Views: 669

Answers (1)

user529758
user529758

Reputation:

You want to tell your shell to interpret escape sequences correctly. If you're using Bash:

$ ./a.out $'\1'

Upvotes: 4

Related Questions