Converting string to hex

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?
use iota function
How about the hex manipulator?

http://www.cplusplus.com/reference/ios/hex/
Last edited on
programmer007,
I'm not sure how this would help me.
koothkeeper,
How do I use it to assign something to something else?
This is probably overkill but:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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
Thank you.
But I have a problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for (int i = 0; i < numbers.size(); i++)
	{
		cout << numbers[i] << " ";
	}
      //6 67 9 0
	cout << endl;
	int iTemp;
	stringstream is, hs;
	for (int i = 0; i < numbers.size(); i++)
	{
		is << numbers[i];
		is >> iTemp; // convert string to int
		hs << std::hex << iTemp; // push int through hex manipulator
		numbers[i] = hs.str();
	}
	for (int i = 0; i < numbers.size(); i++)
	{
		cout << numbers[i] << " ";
	}
       //6 66 666 6666 
Move line 8 into your loop. Or clear the streams at the end of your loop.
Thank you all.
I'd be grateful if someone link me some good sources of stringstream examples.
Topic archived. No new replies allowed.