reading a string of hex values from command line

This is a discussion on reading a string of hex values from command line within the C Programming forums, part of the General Programming Boards category; Hi, I'm writing a encrypting program that implements the AES algorithm and haven't use C for quite a while. The ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    7

    reading a string of hex values from command line

    Hi,

    I'm writing a encrypting program that implements the AES algorithm and haven't use C for quite a while. The program first ask the user if they would like to cipher or decipher and depending on the input, the program will either cipher a plaintext or decipher a ciphertext. The user will have to enter a 16 byte key and plaintext/ciphertext in hex as the input. Is there a way to read from the command line a string of hex pairs that are entered in one line and store them in an array rather than just repeatedly prompting the user to enter each pair?

    The key would be entered like this on the command line: 8e 73 b0 f7 da 0e 64 52 c8 10 f3 2b e3 45 2b f2 and each hex pair would be stored in an array key[]. So far, I was only able to come up with a for loop that scans each pair.

    Code:
    #include <stdio.h>
    unsigned char choice;
    unsigned char key[16], plaintext[16], cipher[16], state[4][4], roundKey[240];
    
    void main() {
        int i;
        printf("Enter 'c' to cipher, 'd' to decipher:  ");
    
        while(choice != 'c' && choice != 'd') {
            choice = getc(stdin);  
            switch(choice) {
                case 'c':
                    printf("Enter the key:  ");
                    /*get key*/
                    for(i=0;i<Nk*4;i++) {
                        scanf("%x", &key[i]);
                    }
                  
                    printf("Please enter the plaintext you like to cipher:  ");
                    break;
                case 'd':
                    printf("Please enter the key to decipher:  ");
                    break;
                default:
                    getc(stdin); /*clear stdin of /n character*/
                    printf("Enter 'c' to cipher, 'd' to decipher:  ");
                    break;
            }
        }
    }
    Right now, I'm just working on the ciphering portion so don't mind the deciphering portion

    Thanks
    Last edited by peeweearies; 02-24-2009 at 12:50 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by peeweearies
    Is there a way to read from the command line a string of hex pairs that are entered in one line and store them in an array rather than just repeatedly prompting the user to enter each pair?
    Just to check: do you know how to read in command line arguments?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    7
    I can use argv to get the entered input from the command line arguments. But since both key and plaintext/ciphertext will be in hex, I wanted to see if there was another option since it would be one very long line of arguments.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    hmm... now I am confused. Are you asking for help on how to read and parse the input from the command line, or are you asking for suggestions on alternative methods of input?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    7
    I don't need to do parsing, just looking for an alternative method to read an input of 16 pairs of hex values and store it in an array of 16 indexes. You see, i have to get an input called key which consists of 16 pairs of hex values (ex.8e 73 b0 f7 da 0e 64 52 c8 10 f3 2b e3 45 2b f2) and another input call plaintext/ciphertext which consists of another 16 pairs of hex values.

    The thing I'm wondering is if there is a method to read the 16 pairs of hex values that is all entered in a single line from the command line. Right now, with scanf, I can only read one hex value at a time. That's why I have a for loop to prompt the user to enter a pair of hex values 16 times(in the for loop, Nk = 4). If I have the user enter all the info using the command arguments, it would be too long since it would consist of 32 arguments for the key and plaintext/ciphertext + 1 argument that determines if the program to cipher or decipher the plaintext/ciphertext and + 1 argument which calls the program itself.
    Last edited by peeweearies; 02-24-2009 at 12:52 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    You could read from and output to files, then simply have the user provide the filenames via the command line.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    7
    That is one option, but I don't want to do the extra coding to check for valid/invalid, opening and closing the file.
    Last edited by peeweearies; 02-24-2009 at 01:02 AM.

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by peeweearies View Post
    That is one option, but I don't want to do the extra coding to check for valid/invalid, opening and closing the file.
    Then make user to redirect stdin from file - and read data from stdin
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  2. How to rearrange values in a string?
    By Sir Andus in forum C Programming
    Replies: 6
    Last Post: 11-21-2006, 01:57 AM
  3. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21