简体   繁体   English

如何从 C++ 中每个十六进制字符字节的十六进制字符串创建一个 0x 附加十六进制字符串?

[英]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试图转换我的以下 go 代码问题

How to create a 0x appended hex string from a hex string for each byte of hex characters in golang? go - 如何为golang中的每个十六进制字符字节从十六进制字符串创建一个0x附加十六进制字符串?

to C++ - but completely lost.到 C++ - 但完全丢失了。

#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.正如您的评论所说,您的函数 appendX() 将仅在输入字符串的末尾附加一个“X”。

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.但是,如果您尝试在字符串中的每个十六进制字节后附加“0x”(如您提到的 GO 语言问题中所述),则您应该在输入字符串中的每 2 个字符后附加“0x”。 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):使用 Ranges V3 库(将来所有这些函数都应该在 C++ 标准库中可用):

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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