Hi ,

I want to read in a file that has string in it and i want to convert all those strings to hex and want to find a pattern.

For Example,

string = "dsofijdsoifjaslifdjaslidjlasjdlasjdlsaj"
and i want to find a sequence of something in Hex: " 5A 1b 1b"
which could be for example: "dsofijdsoifjaslifdjaslidjlasjdlasjdlsaj" in the whole string.

Thanks a lot

Member Avatar for iamthwee

Do a search of the forums / code snippets. This question has been asked a lot of times.

I found how to convert the string into a hex but i don't know how i can do a searc of the exact string... for example i have a string "IJKJIKK*II*IJKJJIK" and i want to search for the portion of the string which has the hex conversion of "49 49 2A" which is in this case "II*" part of the string so i take the whole string as character and thn do search one by one and i want it to stop when it encounters "49 49 2A" but i can not find a method of doing that. I want to stop searching when the seuesnce of 49 49 2A is encountered.

Member Avatar for iamthwee

show us your code so far.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <cstdio>

using namespace std;

int main()
{
    int len;
    string Segment_String,Segment_String1;
    fstream fin2;

    fin2.open("DataFile.txt");
    do
    {
        char Segment[500], buffer[500]="", *pbuffer = buffer;
        fin2 >> Segment;
        len = strlen( Segment );

        for( int i = 0; i < len ;i++ )
        {
            itoa (Segment[i],pbuffer,16);
            pbuffer +=2;
        Segment_String = buffer;
            if (Segment_String == "49492a")
            {
              cout<<Segment_String;
              system("PAUSE");
            }
        }
    }
    while (!fin2.eof());
}

However i am not able to retrieve the "49492a" segment if it is embedded inside a string

For example if i have a string that is II* I will be able to print out 49492a but if the string is jsjdksII*KD then i would not be able to get the print of 49492a.

Any Help will be greatly appreciated.

Waiting for informative reply.

Member Avatar for iamthwee

well for starters itoa and eof is not a good idea to use.

However, i guess you would have understood my problem now and i am not sure how else i would be able to deal with this problem. However, if there is a way to do it without using itoa or eof. Also , really sorry i was not sure how to get my code in properly so here is again.

+===================================

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <cstdio>

using namespace std;

int main()
{
int len;
string Segment_String,Segment_String1;
fstream fin2;

fin2.open("DataFile.txt");
do
{
char Segment[500], buffer[500]="", *pbuffer = buffer;
fin2 >> Segment;
len = strlen( Segment );

for( int i = 0; i < len ;i++ )
{
itoa (Segment[i],pbuffer,16);
pbuffer +=2;
Segment_String = buffer;
if (Segment_String == "49492a")
{
cout<<Segment_String;
system("PAUSE");
}
}
}
while (!fin2.eof());
}

However i am not able to retrieve the "49492a" segment if it is embedded inside a string

For example if i have a string that is "II*" I will be able to print out "49492a" but if the string is "jsjdksII*KD" then i would not be able to get the print of "49492a".

Any Help will be greatly appreciated.

Waiting for informative reply.

Member Avatar for iamthwee

I dunno can't u just seach for the II* instead of the hex version.

http://www.cppreference.com/stdstring/strstr.html

You could look at this site to see if it's an example of what you are trying to do. There is a convenient conversion from string to C style string if there isn't a similar method built into the string class.

I cant since they can be mixed inside a string too for example it could have "II*" and "saljdlsadlkasjdII*jsaldlksad" as string input how will i know if i take this part in as a string that it contains II*

Member Avatar for iamthwee

@ lerner conversion from std::string to cstring could be .c_str()

Member Avatar for iamthwee

I cant since they can be mixed inside a string too for example it could have "II*" and "saljdlsadlkasjdII*jsaldlksad" as string input how will i know if i take this part in as a string that it contains II*

Yeah well you're looking for a substring?

for std::strings...
http://www.cppreference.com/cppstring/find.html

Read the below if you are really intent on using hex?
http://www.daniweb.com/code/snippet488.html

Maybe you could play with that and substrings to get your desired results...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.