Help please!

I'm doing a ascii - hex converter and everything works I just can't figure out how to make the output a 32bit hex string.

For example I want to convert pleasehelp I get 706C6561736568656C70 But I need the output format to be -
706C6561 73656865
6C70

Any help is much appreciated.

Below is my code!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
	string text;
	cout << "Type ascii here:";
	cin >> text;
	cout << "\n\n\n";
	for(int i = 0 ; i < text.length() ; i++)
{
	cout<<hex<<uppercase<<(int)text[i] << "";
	
}	
	cout << "\n\n\n";
	system("pause");
	return 0;
}


Add these lines after your cout but still in your for loop:
1
2
      if ((i + 1) % 4 == 0)
         cout << " ";


Remember, a character is 8bits, or 1 byte, and there is 4 chars per 32bit segment. I hope this helps.
I have not understood exactly what you want. But maybe you need something as

1
2
3
4
5
6
7
8
9
10
11
{
	std::string s = "This is a test";

	for ( std::string::size_type i = 0, len = s.length() ; i < len ; i++ )
	{
		std::cout << std::hex << ( int ) s[i];
		if ( i % 8 == 7 ) std::cout << std::endl;
		else if ( i % 4 == 3 ) std::cout << ' ';
	}
	std::cout << std::endl;
}
Last edited on
Thank you everyone for the help. Volatile's example is exactly what I was looking for.

Thanks again for all the help! :D
Topic archived. No new replies allowed.