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

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.

#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.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

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.

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*

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...

This article has been dead for over six months. Start a new discussion instead.