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

  • Home
  • SEARCH
  • 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 8565707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:29:19+00:00 2026-06-11T17:29:19+00:00

I’d like to take the next two hex characters from a stream and store

  • 0

I’d like to take the next two hex characters from a stream and store them as the associated associated hex->decimal numeric value in a char.

So if an input file contains 2a3123, I’d like to grab 2a, and store the numeric value (decimal 42) in a char.

I’ve tried

char c;
instream >> std::setw(2) >> std::hex >> c;

but this gives me garbage (if I replace c with an int, I get the maximum value for signed int).

Any help would be greatly appreciated! Thanks!

edit: I should note that the characters are guaranteed to be within the proper range for chars and that the file is valid hexadecimal.

  • 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. Editorial Team
    Editorial Team
    2026-06-11T17:29:21+00:00Added an answer on June 11, 2026 at 5:29 pm

    OK I think dealing with ASCII decoding is a bad idea at all and does not really answer the question.

    I think your code does not work because setw() or istream::width() works only when you read to std::string or char*. I guess it from here

    How ever you can use the goodness of standard c++ iostream converters. I came up with idea that uses stringstream class and string as buffer. The thing is to read n chars into buffer and then use stringstream as a converter facility.

    I am not sure if this is the most optimal version. Probably not.

    Code:

    #include <iostream>
    #include <sstream>
    
    int main(void){
        int c;
        std::string buff;
        std::stringstream ss_buff;
        std::cin.width(2);
        std::cin >> buff;
        ss_buff << buff;
        ss_buff >> std::hex >> c;
        std::cout << "read val: " << c << '\n';
    }
    

    Result:

    luk32@genaker:~/projects/tmp$ ./a.out 
    0a10
    read val: 10
    luk32@genaker:~/projects/tmp$ ./a.out 
    10a2
    read val: 16
    luk32@genaker:~/projects/tmp$ ./a.out 
    bv00   
    read val: 11
    luk32@genaker:~/projects/tmp$ ./a.out 
    bc01
    read val: 188
    luk32@genaker:~/projects/tmp$ ./a.out
    01bc
    read val: 1
    

    And as you can see not very error resistant. Nonetheless, works for the given conditions, can be expanded into a loop and most importantly uses the iostream converting facilities so no ASCII magic from your side. C/ASCII would probably be way faster though.

    PS. Improved version. Uses simple char[2] buffer and uses non-formatted write/read to move data thorough the buffer (get/write as opposed to operator<</operator>>). The rationale is pretty simple. We do not need any fanciness to move 2 bytes of data. We ,however, use formatted extractor to make the conversion. I made it a loop version for the convenience. It was not super simple though. It took me good 40 minutes of fooling around to figure out very important lines. With out them the extraction works for 1st 2 characters.

    #include <iostream>
    #include <sstream>
    
    int main(void){
        int c;
        char* buff = new char[3];
        std::stringstream ss_buff;
        std::cout << "read vals: ";
        std::string tmp;
        while( std::cin.get(buff, 3).gcount() == 2 ){
           std::cout << '(' << buff << ") ";
           ss_buff.seekp(0); //VERY important lines
           ss_buff.seekg(0); //VERY important lines
           ss_buff.write(buff, 2);
           if( ss_buff.fail() ){ std::cout << "error\n"; break;}
           std::cout << ss_buff.str() << ' ';
           ss_buff >> std::hex >> c;
           std::cout << c << '\n';
        }
        std::cout << '\n';
        delete [] buff;
    }
    

    Sample output:

    luk32@genaker:~/projects/tmp$ ./a.out
    read vals: 0aabffc
    (0a) 0a 10
    (ab) ab 171
    (ff) ff 255
    

    Please note, the c was not read as intended.

    I found everything needed here http://www.cplusplus.com/reference/iostream/

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have two tables with like below codes: Table: Accounts id | username |
I would like to count the length of a string with PHP. The string
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:

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.