|
You last visited: Today at 22:56
Advertisement
STRING PROBLEM!
Discussion on STRING PROBLEM! within the C/C++ forum part of the Coders Den category.
04/21/2012, 23:05
|
#16
|
elite*gold: 0
Join Date: Dec 2011
Posts: 367
Received Thanks: 199
|
but result : 74657374æ╬J
help me please!
i have a string whit hex number, i do convert to text!
|
|
|
04/21/2012, 23:48
|
#17
|
elite*gold: 5
Join Date: Sep 2006
Posts: 385
Received Thanks: 218
|
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
|
#18
|
elite*gold: 0
Join Date: Oct 2008
Posts: 1,637
Received Thanks: 1,119
|
Quote:
Originally Posted by Nightblizard
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
|
#19
|
elite*gold: 5
Join Date: Sep 2006
Posts: 385
Received Thanks: 218
|
Quote:
Originally Posted by HeavyHacker
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. ****, 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
|
#20
|
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,904
Received Thanks: 25,394
|
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
|
#21
|
elite*gold: 0
Join Date: Dec 2011
Posts: 367
Received Thanks: 199
|
i have complete! thanks friends
|
|
|
|
|
Similar Threads
|
C++ String Problem
04/05/2012 - C/C++ - 6 Replies
Ich stehe vor einem blöden problem,
wahrscheinlich ist die Lösung ziemlich simple aber habe keine Idee wie ich das umsetzen soll undzwar habe ich einen std::string mystring "000111110000110010111100" und ich möchte dazwischen / dahinter die length einfügen so das , dass wie folgt aussieht:
length 3 (000) length 5(11111) length 4 (0000) length 2(11) length 2 (00) length 1 (1) length 1 (0) length 4(1111) length 2(00) am ende sollte der string so aussehen:
...
|
String problem
10/03/2011 - CO2 Programming - 9 Replies
Is not much of a conquer problem, but more a programming problem, but it's related to Conquer as it's for an ani editor.
So the problem is, when I'm reading the ids of the files, then the result is something like this:
561359
1
I have tried split with \n and check if the current char was alpha only, but it still does it :/
|
[VB08]String in String mit mehreren Funden
08/08/2011 - .NET Languages - 6 Replies
Hey,
bin gerade auf ein Problem gestoßen, an dem ich mir seit 3 Stunden die Zähne ausbeiße.
Ich will eine Funktion schreiben, die der _StringBetween Funktion von AutoIt gleich ist. _StringBetween gibt in einem Array alle Strings zwischen zwei SubStrings und dem ganzen String aus. Die Ausgabe bei
_StringBetween("<h1>test1</h1>&l t;h1>test2</h1>", "<h1>", "</h1>") wäre also idealer Weiße ein Array (x = "test1", x = "test2")...
da man in VB08 kein Array returnen kann, komme ich aber einfach...
|
[C++] string zwischen string
11/11/2010 - C/C++ - 6 Replies
tag gibts direkt ne funktion, mit der man einen passenden string zwischen dem string suchen kann?
also meine net .find() sondern sowas ähnliches, die in diesem beispiel "mein string sucht"
Bsp:
"<span id=\"lalala\">"+string mein_string+"</span>"
understanden? :-)
|
String.au3
09/11/2010 - AutoIt - 2 Replies
Hey,
hat jemand die Datei für mich?
Ich finde im Internet nichts (ich hoffe ich habe nichts übersehn)
Mfg
|
All times are GMT +2. The time now is 22:56.
|
|