Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 136739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:59:45+00:00 2026-05-11T06:59:45+00:00

Can’t exactly find a way on how to do the following in C/C++. Input

  • 0

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.

cplusplus ref

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:59:46+00:00Added an answer on May 11, 2026 at 6:59 am

    %x reads an unsigned int, not a uint16_t (thought they may be the same on your particular platform).

    To read only one byte, try this:

    uint32_t byteTmp; scanf('%2x', &byteTmp); uint8_t byte = byteTmp; 

    This reads an unsigned int, but stops after reading two characters (two hex characters equals eight bits, or one byte).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone thoroughly explain the last line of the following code: def myMethod(self): #
Can anyone give me an example of how to return the following json simply
Can anybody tell what is the best(easy) way to sort the following string 'index'
Can I order my users in the database, so I don't have to say
Can anyone enlighten me to a way I can Highlight the content of an
Can I be sure about the order in a Python dictionary? The function op.GetTangent(id)
can someone explain why the compiler accepts only this code template<typename L, size_t offset,
Can't figure out how to do this in a pretty way : I have
Can somebody help me construct this regular expression, please... Given the following strings... "April
Can't work out a way to make an array of buttons in android. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.