Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Correct/Best way to do convert int to string as uppercase hex ?

Correct/Best way to do convert int to string as uppercase hex ?

Scheduled Pinned Locked Moved ATL / WTL / STL
questionlounge
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Defenestration
    wrote on last edited by
    #1

    The following code will generate a random integer, then format this in uppercase hexidecimal, and then copy it to a std:string. Is there a better way of doing formatted output to a std:string ? std::string sRandomString; std::ostringstream osRandomString; int x; x = (rand() << 16) | rand(); osRandomString << std::hex << std::uppercase << x; // formatted as uppercase unsigned hexadecimal integer sRandomString = osRandomString.str(); Why does it not work correctly when I use std::ostringstream::hex and std::ostringstream::uppercase ? If I want to create multiple random strings by using a loop, am I right in thinking I just need to place the following line at the end of the loop, so that the next string generated will overwrite the previous string in the stream, instead of being appended to it ? osRandomString.seekp(0); Is it necessary to place << endl or << ends after x where the formatting takes place ? eg. osRandomString << std::hex << std::uppercase << x << endl; If yes, should I use endl or ends ?

    S 1 Reply Last reply
    0
    • D Defenestration

      The following code will generate a random integer, then format this in uppercase hexidecimal, and then copy it to a std:string. Is there a better way of doing formatted output to a std:string ? std::string sRandomString; std::ostringstream osRandomString; int x; x = (rand() << 16) | rand(); osRandomString << std::hex << std::uppercase << x; // formatted as uppercase unsigned hexadecimal integer sRandomString = osRandomString.str(); Why does it not work correctly when I use std::ostringstream::hex and std::ostringstream::uppercase ? If I want to create multiple random strings by using a loop, am I right in thinking I just need to place the following line at the end of the loop, so that the next string generated will overwrite the previous string in the stream, instead of being appended to it ? osRandomString.seekp(0); Is it necessary to place << endl or << ends after x where the formatting takes place ? eg. osRandomString << std::hex << std::uppercase << x << endl; If yes, should I use endl or ends ?

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      If you want to use STL strings, you might want to look at Boost.Format[^]. Then you could do something like:

      osRandomString = boost::format("%X") % x;

      It's basically a type-safe and more C++-ish version of printf. And there's so much other good stuff in Boost as well that you're doing yourself a favour by using it (IMO!).

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups