Can’t exactly find a way on how to do the following in C/C++.
Input : hexdecimal values, for example: ffffffffff…
I’ve tried the following code in order to read the input :
uint16_t twoBytes; scanf('%x',&twoBytes);
Thats works fine and all, but how do I split the 2bytes in 1bytes uint8_t values (or maybe even read the first byte only). Would like to read the first byte from the input, and store it in a byte matrix in a position of choosing.
uint8_t matrix[50][50]
Since I’m not very skilled in formating / reading from input in C/C++ (and have only used scanf so far) any other ideas on how to do this easily (and fast if it goes) is greatly appreciated .
Edit: Found even a better method by using the fread function as it lets one specify how many bytes it should read from the stream (stdin in this case) and save to a variable/array.
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
Parameters
ptr – Pointer to a block of memory with a minimum size of (size*count) bytes.
size – Size in bytes of each element to be read.
count – Number of elements, each one with a size of size bytes.
stream – Pointer to a FILE object that specifies an input stream.
%xreads anunsigned int, not auint16_t(thought they may be the same on your particular platform).To read only one byte, try this:
This reads an
unsigned int, but stops after reading two characters (two hex characters equals eight bits, or one byte).