-3

Possible Duplicate:
How to convert a number to string and vice versa in C++

In C++ how do you convert a hexadecimal integer into a string representation of the decimal value of the original hex?

Let us say we have an integer whose hexadecimal representation is a1a56 (which in decimal equals 662102) and you want to convert this to a string "662102"

How would you solve this?

ps: i suggest a functional solution as an answer, feel free to shoot it down (in a polite manner, if possible)

Community
  • 1
  • 1
tony gil
  • 9,424
  • 6
  • 76
  • 100
  • So the hexadecimal number is orignally what type? String? int? If it's originally int then just print it out. – nhahtdh Oct 12 '12 at 02:30
  • @nhahtdh it is int BUT i do not want to print it out, i need to manipulate the string after (let us say i am putting together a query for mysql). thanx 4 the input! – tony gil Oct 12 '12 at 02:39
  • 3
    @tonygil then it is not an "hexadecimal integer". It's an integer. *Numbers* have no bases. Representations do. – R. Martinho Fernandes Oct 12 '12 at 02:39
  • @R.MartinhoFernandes i was just editing as per your suggestion. but you override me big time! thanx for the help clarifying the question. the only problem is that, even though it is technically correct, people search for the original title: "How to convert hex to string" (that's what's going on google). – tony gil Oct 12 '12 at 02:45
  • @R.MartinhoFernandes not really a duplicate, i checked that question before posting this and the answers here address a specific situation whereas that excellent question is more extensive in proposing multiple solutions for various scenarios. try googling (or internal searching) this "stackoverflow c++ convert hex string" – tony gil Oct 12 '12 at 02:52
  • 1
    @tony there's no "specific situation" at hand here. It's the same thing, except for a misunderstanding of the concept of *number*. Feel free to rollback to the old text if you think that helps with search. – R. Martinho Fernandes Oct 12 '12 at 02:53
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17904/discussion-between-tony-gil-and-r-martinho-fernandes) – tony gil Oct 12 '12 at 02:58

4 Answers4

9

You can use stringstreams:

int x = 0xa1a56;
std::stringstream ss;
ss << x;
cout << ss.str();

Or if you prefer a function:

std::string convert_int(int n)
{
   std::stringstream ss;
   ss << n;
   return ss.str();
}

Edit: Make sure you #include <sstream>

GWW
  • 43,129
  • 11
  • 115
  • 108
6

You can read the number from a string stream as hex, and write it back to a different string stream as decimal:

int x;
istringstream iss("a1a56");
iss >> hex >> x;
ostringstream oss;
oss << x;
string s(oss.str());

Link to ideone.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
4

The simplest way of doing this, using the latest version of the C++ Standard (C++11), is to use the std::to_string function:

#include <iostream>
#include <string>

int main()
{
  /* Convert: */
  std::string s { std::to_string(0xa1a56) };

  /* Print to confirm correctness: */
  std::cout << s << std::endl;
  return 0;
}
jogojapan
  • 68,383
  • 11
  • 101
  • 131
2
std::string HexDec2String(int hexIn) {
  char hexString[4*sizeof(int)+1];
  // returns decimal value of hex
  sprintf(hexString,"%i", hexIn); 
  return std::string(hexString);
}
tony gil
  • 9,424
  • 6
  • 76
  • 100