简体   繁体   中英

How to create a 0x appended hex string from a hex string for each byte of hex characters in C++?

Trying to convert my following go code question

How to create a 0x appended hex string from a hex string for each byte of hex characters in golang?

to C++ - but completely lost.

#include <iostream> 
#include <string> 
using namespace std; 

// Function to append 0x
void appendX(string str) 
{ 
    // Appends 1 occurrences of "X" to str 
    str.append(1, 'X'); 
    cout << "Modified append() : "; 
    cout << str; 

} 


int main() 
{ 
    string str("01234567891011121314151617181920"); 

    cout << "String : " << str << endl; 
    appendX(str); 

    return 0; 
} 

Your function appendX() is going to append only one 'X' at the end of your input string, as your comment also says.

But if you are trying to append "0x" after every hex byte within your string, (as asked in the GO language question you mentioned), you should be appending "0x" after every 2 characters in your input string. Try below:

void appendX(String str) 
{ 
    String outstr;
    for(int i=0;i<str.size();i=i+2)
    {   
        outstr.append("0x"); 
        outstr.append(str,i,2);
    }
    cout << "Modified append() : "; 
    cout << outstr; 
} 

Simplest/Understandable way is to iterate over a string and add the desirable amount of characters to a result string.

std::string insertStringInto(const std::string& s, const int interval, const std::string& sep_str)
{
    std::string result_str;

    auto chars_count_until_sep = interval;

    for (auto ch: s)
    {
        if (chars_count_until_sep == 0)
        {
            result_str += sep_str;
            chars_count_until_sep = interval;
        }

        result_str += ch;
        --chars_count_until_sep;
    }

    return result_str;
}


int main()
{
    std::string str("01234567891011121314151617181920");
    std::cout << "String : " << insertStringInto(str,2,", 0x") << std::endl;
    return 0;
}

Using the Ranges V3 library (in the future all these functions should be available in the C++ standard library):

std::string insertStringInto(const std::string& in_string, const int interval, const std::string& sep_str)
{
    using namespace ranges::views;

    auto concat_to_str = [](auto grp) { // auxilllary function to concat single characters into a string
        return ranges::accumulate(grp, std::string {});
    };

    auto r = in_string                  // Use the characters from the input string parameter
             | chunk(interval)          // Split string up into interval count of characters
             | transform(concat_to_str) // combine characters from chunks into strings
             | intersperse(sep_str);    // intersperse between the chunks the separator text

    return concat_to_str(r); // concat all the strings into 1 string
}

int main()
{
    std::string str("01234567891011121314151617181920");

    std::cout << "String : " << insertStringInto(str, 2, ", 0x") << std::endl;

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM