STRING PROBLEM!

04/20/2012 20:44 Kingrap#1
:cool: Hi community,
i have a problem whit the string in c++ !

Example:

string data("1234567890");

to keep the number 1 and delete numer 2?

Result: 13579

from the first to the end! :)

help me please :handsdown: ...
04/20/2012 21:06 omer36#2
Code:
string data = "1234567890";


for (int i=1; i< data.size(); i += 2){
    cout << i << endl;
}
so?
04/20/2012 21:16 MrSm!th#3
Quote:
Originally Posted by omer36 View Post
Code:
string data = "1234567890";


for (int i=1; i< data.size(); i += 2){
    cout << i << endl;
}
so?
you just give them out, you dont save them to the string
04/20/2012 21:18 Kingrap#4
No..

Example:

string data = "19A546D7482250A1R6T";

-------------------------------------------------------------------------------------
the data is random! xD
-------------------------------------------------------------------------------------

RESULT: 1A4D425ART

-------------------------------------------------------------------------------------
one yes, one no, one yes, one no..
-------------------------------------------------------------------------------------

Help me please :(
04/20/2012 21:45 MrSm!th#5
Quote:
Originally Posted by Kingrap View Post
No..

Example:

string data = "19A546D7482250A1R6T";

-------------------------------------------------------------------------------------
the data is random! xD
-------------------------------------------------------------------------------------

RESULT: 1A4D425ART

-------------------------------------------------------------------------------------
one yes, one no, one yes, one no..
-------------------------------------------------------------------------------------

Help me please :(
thats exactly what he wrote, just without saving it to another string

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

for(int i=0; i<data.length(); i += 2)
    result.push_back(data[i]);
04/20/2012 22:05 omer36#6
Ach, lol was fürn fail hab ich denn da geschrieben, das ging nur zufällig gut, weil der String von 1--9 ging...Und ich einfach nur aus der Schleife jede 2. Zahl ausgegeben habe..


Like this?

Code:
#include <iostream>
#include <conio.h>

using namespace std;

int main(){

    string data = "A12BC34E56D4S";
    char split[data.size()];

    for (int i=0; i<data.size(); i+=2){
        split[i] = data.at(i);
    }

    for (int i=0; i<sizeof(split); i ++){
        cout << split[i];
    }


    _getch();

}
04/20/2012 22:29 MrSm!th#7
Das ist noch größerer Käse.
1. ist es Speicherplatzverschwendung den neuen String so groß wie den alten zu machen
2. ist jedes 2. Zeichen ein random Zeichen bei dir, weil du i als Index für den Ziel Buffer nimmst

Warum du ein char Array anstatt eines Strings nimmst, ist mir nicht ganz klar und at() verbraucht nur unnötig Laufzeit, weil es den Index prüft. Da du in der Schleife ohnehin abbrichst, wenn du das Ende erreicht hast, kannst du den ungeprüften [] Operator nutzen.

Ich würde übrigens jemanden mit besserem Wissen über die STL als ich es habe bitten eine weitere Version zu machen, vielleicht mit Iteratoren.
Meine kommt mir so unschön vor, hab aber auf die Schnelle nix anderes an String Methoden für dieses Vorgehen gefunden und ich war mir nicht sicher, ob Iteratoren mit +=2 oder dem % Operator umgehen können.
04/20/2012 23:35 Kingrap#8
Perfect!! Thanks friends!

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

for(int i=0; i<data.length(); i += 2)
    result.push_back(data[i]);
Now i have another problem :( ,

i got hex data of the string (im woking whit a password of a game xD)..

RESULT = 74657374

that is : 74 65 73 74
t e s t

-----------------------------------------
HEX TO CHAR
-----------------------------------------

how to convert hex to char of the "RESULT" string?

Example:

string hex = "74657374"

RESULT = test..

after this i haven't other questions :) .

Help me please, Thanks again! :)

-----------------------------------------
CHAR AND HEX TABLE : [Only registered and activated users can see links. Click Here To Register...]
04/21/2012 00:44 MrSm!th#9
I dont understand your problem.
Every character is just represented by a numerical value.

Just convert your Byte Array of hex data into a char array and initialize a string with it and you are done:

Code:
byte pw[] = { 0x74, 0x65, 0x73, 0x74 };

char *cstr = new char[sizeof(pw)+1];
memcpy(cstr, pw, sizeof(pw));
cstr[sizeof(pw)] = '\0';

string str(cstr);

//done
04/21/2012 10:09 Tyrar#10
Quote:
Originally Posted by MrSm!th View Post
Ich würde übrigens jemanden mit besserem Wissen über die STL als ich es habe bitten eine weitere Version zu machen, vielleicht mit Iteratoren.
Meine kommt mir so unschön vor, hab aber auf die Schnelle nix anderes an String Methoden für dieses Vorgehen gefunden und ich war mir nicht sicher, ob Iteratoren mit +=2 oder dem % Operator umgehen können.
iteratoren müssten genau so mit +=2 umgehen können wie fast alles andere auch :)

Code:
std::string result;
result.reserve(data.capacity()/2+1);
for(std::string::const_iterator it=data.begin(); it!=data.end(); it+=2)
result.push_back(*it);
müsste also funktionieren :)

Quote:
Originally Posted by MrSm!th View Post
Code:
byte pw[] = { 0x74, 0x65, 0x73, 0x74 };

char *cstr = new char[sizeof(pw)+1];
memcpy(cstr, pw, sizeof(pw));
cstr[sizeof(pw)] = '\0';

string str(cstr);

//done
ich glaub er meinte von einem hex string in einen normalen string zu konvertieren: std::string("74657374") soll am ende "test" ergeben.
ich würde das ganze auf anhieb so probieren:
Code:
std::string result;

for(uint32_t i=0; i<data.size(); ++i) {
    char c1[2]={0};
    c1[0]=data[i];
    char c2[2]={0};
    c2[0]=data[++i]; // i zuerst erhöhen, dann verwenden? bin mir grad nich sicher
    char b[2]={0};
    b[0]=(strtol(c1,nullptr,16)<<4)|strtol(c2,nullptr,16);
    result.append(b);
}
// untested!
das ganze nicht getestet, lässt sich eher als pseudo code lesen :)
04/21/2012 11:21 MrSm!th#11
Wäre für mich sehr seltsam, wenn das Passwort in Hex Zeichen als String Literal dargestellt würde oO

Und was soll dieser komplizierte Code? :O Viel zu viel C :/

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

for(int i=0; i<data.length(); i += 2)
{
    string tmp = data.substr(i, i+1);
    char c = strtol(tmp.c_str(), nullptr, 0);
    result.push_back(c);
}
Ginge vermutlich noch leichter und ohne strtol mithilfe eines stringstreams.
04/21/2012 14:20 Kingrap#12
ich glaub er meinte von einem hex string in einen normalen string zu konvertieren: std::string("74657374") soll am ende "test" ergebe

yes, hex string to normal string..

std::string("74657374");
the normal string must be : test..
the number: 74 65 73 74 are hex..

sorry for my english but im italian :)

HeavyHacker there is a error in the code.. :(
MrSm!th there is a error in the code.. :(

my code is..

Code:
#include <iostream>
#include <string>

using namespace std;

int main(){

	string password("0E4S7T4C6D50743C7O4");

	if (password.length()%2 == 0) {
		string password4(password.substr(4,password.length()));
		string result;
		result.reserve(password4.capacity()/2 +1);
		for(int i=0; i<password4.length(); i += 2)
		result.push_back(password4[i]);
		cout << "Password hex: " << result;
	} else {
		string password3(password.substr(3,password.length()));
		string result;
		result.reserve(password3.capacity()/2 +1);
		for(int i=0; i<password3.length(); i += 2)
		result.push_back(password3[i]);
		cout << "Password hex: " << result;
	}

	getchar();
}
Help me please!
04/21/2012 21:56 MrSm!th#13
But S is not a valid hex digit oO

Quote:
yes, hex string to normal string..

std::string("74657374");
the normal string must be : test..
the number: 74 65 73 74 are hex..
that would be
Code:
string result;
result.reserve(data.capacity()/2 + 1);

for(int i=0; i<data.length(); i += 2)
{
    string tmp = data.substr(i, i+1);
    char c = strtol(tmp.c_str(), nullptr, 0);
    result.push_back(c);
}
what "error" do you mean?
04/21/2012 21:59 Kingrap#14
nullptr is error
04/21/2012 22:32 Nightblizard#15
Then you're using an old compiler. Replace it with 0 or NULL.