title: C/C++ read a byte from an hexinput from stdin
body: <p>Can't exactly find a way on how to do the following in C/C++.</p>
  
  <p>Input : hexdecimal values, for example: ffffffffff...</p>
  
  <p>I've tried the following code in order to read the input :</p>
  
  <pre><code>uint16_t twoBytes;
  scanf("%x",&amp;twoBytes);
  </code></pre>
  
  <p>Thats works fine and all, but how do I split the 2bytes in 1bytes <code>uint8_t</code> 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.</p>
  
  <pre><code>uint8_t matrix[50][50]
  </code></pre>
  
  <p>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 .</p>
  
  <p>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.</p>
  
  <pre><code>size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
  </code></pre>
  
  <p>Parameters</p>
  
  <p>ptr - Pointer to a block of memory with a minimum size of (size*count) bytes.</p>
  
  <p>size - Size in bytes of each element to be read.</p>
  
  <p>count - Number of elements, each one with a size of size bytes.</p>
  
  <p>stream - Pointer to a FILE object that specifies an input stream. </p>
  
  <p><a href="http://www.cplusplus.com/reference/clibrary/cstdio/fread.html" rel="nofollow noreferrer"> cplusplus ref</a></p>