STRING PROBLEM!

04/21/2012 23:05 Kingrap#16
but result : 74657374æ╬J

help me please!
i have a string whit hex number, i do convert to text!
04/21/2012 23:48 Nightblizard#17
I would do it this way:
Code:
std::string hexStringToString(std::string data)
{
	std::string result;
	result.reserve(data.length());

	for(size_t i = 0; i < data.length(); i+=2)
	{
		unsigned long x = 0;
		std::stringstream ss;

		ss << std::hex << data.substr(i, 2);
		ss >> x;

		result.append(1, static_cast<char>(x));
	}

	return result;
}
04/22/2012 00:09 Tyrar#18
Quote:
Originally Posted by Nightblizard View Post
I would do it this way:
Code:
std::string hexStringToString(std::string data)
{
	std::string result;
	result.reserve(data.length());

	for(size_t i = 0; i < data.length(); i+=2)
	{
		unsigned long x = 0;
		std::stringstream ss;

		ss << std::hex << data.substr(i, 2);
		ss >> x;

		result.append(1, static_cast<char>(x));
	}

	return result;
}
Code:
std::string hexStringToString(std::string data)
{
	std::stringstream result;

	for(size_t i = 0; i < data.length(); i+=2)
		result << std::hex << data.substr(i, 2);

	return result.str();
}
04/22/2012 00:12 Nightblizard#19
Quote:
Originally Posted by HeavyHacker View Post
Code:
std::string hexStringToString(std::string data)
{
	std::stringstream result;

	for(size_t i = 0; i < data.length(); i+=2)
		result << std::hex << data.substr(i, 2);

	return result.str();
}
Yeah, that is even better!


Edit:
Oh, since he's using an old compiler, he should pass data by reference! No move ctor without C++11. :)

Edit2:
Nope, that doesn't work. Damn, I should have checked it before I've posted this.
What you do is taking the data string, putting it into a stringstream and returning it without modification.
04/22/2012 00:15 MrSm!th#20
As I said, stringsteams would be nicer.

Btw. i found a mistake in my code:

Code:
string result;
result.reserve(data.capacity()/2 + 1);

for(int i=0; i<data.length(); i += 2)
{
    string tmp = data.substr(i, 2);
    char c = strtol(tmp.c_str(), nullptr, 16);
    result.push_back(c);
}
04/22/2012 01:04 Kingrap#21
i have complete! thanks friends :)