how to find and replace some group of characters in a char string with bytes? *basic

Discussion in 'C' started by Owyn, May 11, 2009.

  1. Owyn

    Owyn New Member

    Joined:
    May 11, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    hi, some basic help i need here or not (=

    1. i need to replace every "\4" word with 04 hex byte

    2. i need to replace every "a" symbol with "b" symbol after "\r" word but not after "\e" word,
    replace "a" symbol with "b" symbol just until there is a "\e" word, after "\e" no replaceing "a" symbol with "b" symbol, but if there is an "\r" somewhere after "\e" - so replace

    hope you help me ^^"
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Re: how to find and replace some group of characters in a char string with bytes? *ba

    Sounds fairly easy. How far have you got and where are you stuck?
    I would implement this using a flag to track whether or not a "\e" (whatever that is) has occurred and replace "a" with "b" only if that flag is set. Also I'd use a counter for the first part and reset it every time I did a substitution. IOW, something like:
    Code:
    flag=1
    counter=0
    get symbol
    counter++
    if counter=4
      symbol=04 hex byte (whatever that means)
      reset counter
    end if
    if symbol="\e"
      flag=0
    else if symbol="\r"
      flag=1
    else if symbol="a" and flag=1
      symbol="b"
    end if
    
    However you need to think about what will happen if the 4n-th symbol is a \e or \r - will the substitution of this for an "04 hex byte" stop the flag being set accordingly? If not, how do you think you might change the above code to fix that?
     
  3. Owyn

    Owyn New Member

    Joined:
    May 11, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Re: how to find and replace some group of characters in a char string with bytes? *ba

    hex i mean... like this +_+ *look attach

    every "\4" should be replaced with 04 hex byte no matter about other things

    sounds like it loop thru every 2 symbols? what if it stops looking at two symbols "h\" for e.g. and next would be "r " so it wont find the "\r" ?

    i'm new at C++ but looping thru symbols and replaceing them looks easy (at least with my knowlegde of PAWN) but i think i'm stuck with condition of replacing and not replaceing if markers were found like i need to mark string from \r to \e (if \e is after, + even if it's not) to replace things and mark string from \e to \r (if \r is after, + even if it's not) to not replace
    i heard C++ has enchanced abilities to work with strings such way but i'm not sure
     

    Attached Files:

  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Re: how to find and replace some group of characters in a char string with bytes? *ba

    What exactly do you mean by a symbol? How many of them can you see on the highlighted line in the screen shot?

    Is there a \4 on the screenshot, and if not could you post a screen shot that shows one?
     
  5. Owyn

    Owyn New Member

    Joined:
    May 11, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Re: how to find and replace some group of characters in a char string with bytes? *ba

    a symbol, a character, a letter
    anyway i think i found how to write bytes
    Code:
    data.push_back(0x04);
     
  6. Owyn

    Owyn New Member

    Joined:
    May 11, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Re: how to find and replace some group of characters in a char string with bytes? *ba

    output for text from test string is "bbcdefbbcdefabcdef"

    when i use for input "hi" it outputs " . hi" (20 2E 14 20 68 69 in hex) and it conver every a to b, and when i input "\\s" it outputs either "sss" or "s\s" o_O

    can you look at my code and maybe tell me something how to fix it?
    Code:
    // Modifier functor.
    struct Modify : public std::unary_function<char, void>
    {
        // Constructor.
        Modify() : slash(false), a_to_b(true)
        {
        }
    
        // () operator.
        void operator () (char c)
        {
            // Did we gat a '\' last time?
            if (slash)
            {
                // Enable 'a' to 'b'
                if (c == 'r')
                {
                    a_to_b = true;
                }
                // Disable 'a' to 'b'
                else if (c == 'e')
                {
                    a_to_b = false;
                }
                // It was a '4'
                else if (c == '4')
                {
                    data.push_back(0x04);
                }
    
                // Done with the '\'
                slash = false;
            }
            else
            {
                // Is the current character a '\' ?
                slash = (c == '\\');
    
                if (!slash)
                {
                    if ((c == 'a') && a_to_b)
                    {
                        // Modify 'a' to 'b'
                        data.push_back('b');
                    }
                    else
                    {
                        // Just copy
                        data.push_back(c);
                    }
                }
            }
        }
    
        // Result.
        std::vector<char> data;
    
    private:
        
        // Flags.
        bool slash;
        bool a_to_b;
    };
    
    
    
    void func_xsay()
    {
    
    	char msg[384];
    
    	for(int i=1; i < args; i++)
    	{
    	sprintf(msg,"%s %s",msg,oEngfuncs.Cmd_Argv(i));
    	}
    
    	// Test string
        std::string text("abcdef\\rabc\\4def\\eabcdef\\4");
    	//text = msg;
    
    
        // Make the data.
        std::vector<char> data(text.begin(), text.end());
    
    
        // Create the functor.
        Modify modify;
    
        // Run the functor over all the data.
        modify = std::for_each(data.begin(), data.end(), modify);
    
        // Read back the modified data.
        data = modify.data;
    
    
    	//make msg = data;
    	for (int i = 0; i < data.size(); i++)
    
    	{
    
    	msg[i] = data[i];
    
    	}
    	
    
    	wsprintf(cmd,"you say %s",msg);
    
    
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice