Safe Input for Hex

This is a discussion on Safe Input for Hex within the C Programming forums, part of the General Programming Boards category; Hello, Im a newbie to the forum but i have an intermediate knowledge of C programming. I am having some ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    Safe Input for Hex

    Hello,

    Im a newbie to the forum but i have an intermediate knowledge of C programming. I am having some trouble getting a hex input of 16-byte into an array.

    So basically i've just finished doing an AES model in C and I need to find a way to allow the user to enter the 16-byte data in an array( for e.g input[16]). i want each array location to hold 1 byte.

    At the moment, I cant get all the 16-byte at one go; allowing user to type in a single line.( im havin to press ENTER after each byte is typed..). Im using scanf ("%2x", &input[i]).

    I hope this make sense to some1 out there..

    Thanks.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Two possibilities pop to mind...

    1) let the user enter it as a string and parse the string
    2) write one hellatious scanf with all 16 array elements as parameters.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,245
    Look up fgets() for reading a line of input (from either standard input or from a file).

    Once you have read the string in, work out a way to check if the contents are what you expect and interpret accordingly.

    One thing to remember is that, by convention, most functions that work with strings in C assume those strings null terminated. For example, passing it to sscanf() requires that. This means if the user is required to enter FEDCBA9876543210 (16 bytes) and your program will treat it as a C string, then your input array needs to have 17 elements. The 17th element is to hold the null terminator (which is a char with value zero, that is something different from the character '0').
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    Thanks for ur replies. I'll certainly looked into that. Cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List from Standard Input
    By mercuryfrost in forum C Programming
    Replies: 14
    Last Post: 08-24-2009, 12:05 AM
  2. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  3. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  4. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 12:22 AM
  5. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM

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