Converting string to hex

Apr 8, 2015 at 1:53pm
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
use iota function
Apr 8, 2015 at 2:00pm
How about the hex manipulator?

http://www.cplusplus.com/reference/ios/hex/
Last edited on Apr 8, 2015 at 2:04pm
Apr 8, 2015 at 2:21pm
programmer007,
I'm not sure how this would help me.
koothkeeper,
How do I use it to assign something to something else?
Apr 8, 2015 at 2:25pm
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 Apr 8, 2015 at 2:26pm
Apr 8, 2015 at 2:46pm
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 
Apr 8, 2015 at 2:52pm
Move line 8 into your loop. Or clear the streams at the end of your loop.
Apr 8, 2015 at 3:21pm
Thank you all.
I'd be grateful if someone link me some good sources of stringstream examples.
Topic archived. No new replies allowed.