0x20 Treated As 0x00 With Stringstream - C++ - Stack Overflow

    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs 0x20 treated as 0x00 with stringstream Ask Question Asked 4 years, 6 months ago Modified 4 years, 6 months ago Viewed 368 times 1

The following program writes 0x20 to stringstream, then reads the value and it's 0. Why is that and how to write a value so it is read as 0x20?

#include <iostream> #include <sstream> int main() { std::stringstream ss; ss << (char) 0x20; char c; ss >> c; if ( c == 0x20 ) { std::cout << "32"; } else if ( c == 0 ) { std::cout << "0"; } else { std::cout << "other"; } std::cout << '\n'; } Share Improve this question Follow asked May 6, 2020 at 13:47 AshleyWilkes's user avatar AshleyWilkesAshleyWilkes 8216 silver badges13 bronze badges 2
  • 3 ss >> std::noskipws >> c; it reads whitespace. By default, any whitespaces are discarded when reading by formatted operation. – rafix07 Commented May 6, 2020 at 13:49
  • thank you very much! – AshleyWilkes Commented May 6, 2020 at 13:51
Add a comment |

1 Answer 1

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 4

By default, std::stringstream's >> operator skips over writespace. And in ASCII (a typical but by no means universal encoding). 0x20 is the space character you get when you press that long button on your keyboard.

As there is nothing else on the stream, NUL is output to c.

Introducing the manipulator std::noskipws is the fix; that is ss >> std::noskipws >> c; reads the value as 0x20.

Share Improve this answer Follow edited May 6, 2020 at 14:16 answered May 6, 2020 at 13:51 Bathsheba's user avatar BathshebaBathsheba 234k35 gold badges368 silver badges495 bronze badges Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • We'll Be In Touch - A New Podcast From Stack Overflow!
  • The app that fights for your data privacy rights
  • Featured on Meta
  • More network sites to see advertising test
  • We’re (finally!) going to the cloud!
  • Call for testers for an early access release of a Stack Overflow extension...
0 C++ Stringstream int to string but returns null 1 C++ stringstream reads all zero's 4 C++ stringstream >> int returns zero 7 Why does stringstream yields strange values? 1 C++ Stringstream assigning wrong value to variable? 5 Why isn't "0" == "0"? 5 Subclassing stringstream gives "0x401bad ABC" instead of "Foo ABC" 1 stringstream adding an extra zero 1 stringstream removes leading 0s 3 c++ std::stringstream gives me weird behavior

Hot Network Questions

  • "That right there is why…”
  • Is is plausible that we could have neuronal maps of human brains without mind uploading being possible?
  • What is it called when you have a hobby where you're good enough at to impress others but you yourself know you're only beginning?
  • How does time dilation affect the synchronization of clocks in different gravitational potentials?
  • Are Quantum Algorithms better than classical algorithms for all problems?
  • Given a profit margin, how to determine the selling price?
  • Gifting $10k to my 17 year old nephew
  • How can I solve a multi-delay differential equation?
  • What are the ethical considerations regarding mandatory class participation?
  • How to utilize zener diode specs
  • block nvme0n1: no uuid available providing old nguid - after disk cloning
  • In what order should I watch the Hunger Games films?
  • Was it really possible to damage my VGA card by programming it in assembly through its latches registers?
  • Quantum gravity and perturbative parameters
  • box several horizontally connected cells in an array. Is there a command similar to \Aboxed?
  • Why does glm in R with family binomial(link="log") sometimes require start values?
  • How did the Dutch Republic get sufficient timber to build its navies?
  • Can a storage device completely erase itself while performing the erase?
  • How were lead sheets made in the Graeco-Roman world?
  • Animated short - brief history of mankind
  • How to handle a campaign where the players are trapped inside a monster controlled by another player?
  • How to DSolve[{-0.8*y'[x]*y''[x]/(1 + (y'[x])^2)^1.4 == 0.77781490, y[0] == 0, y[1] == 1}, y[x], x]
  • Circular modal logic question: Does it makes sense to ask if it is possible for the set of possible worlds to be empty?
  • Why would you not issue orders to a facility?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-cpp

Từ khóa » C 0x20