Difference between revisions of "CPP/IntToHex"

From ProgrammingExamples
< CPP
Jump to: navigation, search
(Created page with '==IntToHex.cxx== <source lang="cpp"> #include <iostream> #include <sstream> int IntToHex(int input); int main(int argc, char* argv[]) { int serial = 40000359; int serialHex…')
 
m (IntToHex.cxx)
 
Line 22: Line 22:
 
{
 
{
 
   std::stringstream ss;
 
   std::stringstream ss;
  std::string result;
+
   ss << input;
   ss << "0x" << input;
+
 
   int output;
 
   int output;
 
   ss >> std::hex >> output;
 
   ss >> std::hex >> output;

Latest revision as of 00:12, 15 June 2011

IntToHex.cxx

#include <iostream>
#include <sstream>
 
int IntToHex(int input);
 
int main(int argc, char* argv[])
{
  int serial = 40000359;
  int serialHex = (int)0x40000359;
  int convertedSerial = IntToHex(serial);
 
  std::cout << serialHex << std::endl;
  std::cout << convertedSerial << std::endl;
 
  return 0;
}
 
 
int IntToHex(int input)
{
  std::stringstream ss;
  ss << input;
  int output;
  ss >> std::hex >> output;
 
  return output;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(IntToHex)
 
ADD_EXECUTABLE(IntToHex IntToHex.cxx)