-
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.
-
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.
-
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').
-
Thanks for ur replies. I'll certainly looked into that. Cheers.