Apr 8, 2015 at 1:53pm UTC  
Hello. I need to convert string of decimal number to hex. I need the string to remain string (or at least the output to be string). What's the easiest way to do this?
 
Apr 8, 2015 at 2:00pm UTC  
Last edited on Apr 8, 2015 at 2:04pm UTC  
 
Apr 8, 2015 at 2:21pm UTC  
programmer007,
 
Apr 8, 2015 at 2:25pm UTC  
This is probably overkill but:
1#include <iostream> 
#include <string> 
#include <sstream> 
int  main()
{
  std::string iString = "12345" , hString;
  int  iTemp = 0;
  std::stringstream is, hs;
  is << iString; 
  is >> iTemp; // convert string to int 
  hs << std::hex << iTemp; // push int through hex manipulator 
  hString = hs.str(); // store hex string 
  std::cout << "Decimal = "  << iString << ", Hex = "  << hString << std::endl;
  return  0;
}
Last edited on Apr 8, 2015 at 2:26pm UTC  
 
Apr 8, 2015 at 2:52pm UTC  
Move line 8 into your loop.  Or clear the streams at the end of your loop.
 
Apr 8, 2015 at 3:21pm UTC  
Thank you all.