【问题标题】:C++ std::string array to unsigned char array conversionC++ std::string 数组到无符号字符数组的转换
【发布时间】:2021-11-01 05:20:04
【问题描述】:

我有一个std::string 数组,我需要将它转换为一个无符号字符数组,以便我可以将此数组与只接受无符号字符数组的第三方库一起使用。

假设我的数组是

std::string array[3];
array[0] = "a105b";
array[1] = "c258e"
array[2] = "ff587";

我需要把这个数组转入:

unsigned char cArray[3][5];

我可以硬连线 unsigned char,如下所示:

unsigned char cArray[3][5] = {"a105b", "c258e", "ff587"};

但我无法找到一种方法来使用 C++ 代码将数据从 std::string 数组传输到 unsigned char 数组。

【问题讨论】:

  • 字符串字面量包括终止符'\0'which your array has not accounted for。您是否打算删除那些终止字符?
  • @Musnazril Mustaq Khan 在与 C 相对的 C++ 中,此声明 unsigned char cArray[3][5] = {"a105b", "c258e", "ff587"};是不正确的。在这种情况下,数组的维度必须是 cArray[3][6]。
  • 你可以使用循环。
  • 你可以memcpy字符串,使用string::c_str方法。

标签: c++ arrays char


【解决方案1】:

您可以创建一个函数,循环遍历两个数组并从一个数组复制到另一个数组。

例子:

#include <algorithm>
#include <iostream>
#include <string>

template<size_t R, size_t N>
void foo(const std::string(&src)[R], unsigned char(&dest)[R][N]) {

    // both `src` and `dest` must be arrays with `R` rows
    // `N` is how many unsigned chars each inner array in `dest` has

    for(size_t idx = 0; idx < R; ++idx) {
        // Copy from `src[idx]` to `dest[idx]`
        // Copy at most `N` chars but no more than the length of the string + 1
        // for the null terminator:
        std::copy_n(src[idx].c_str(), std::min(N, src[idx].size() + 1), dest[idx]);

        // Add the below line if the arrays in cArray are supposed to
        // be null terminated strings:
        //dest[idx][N - 1] = '\0';
    }
}

int main() {
    std::string array[3];

    array[0] = "a105b";
    array[1] = "c258e";
    array[2] = "ff587";

    unsigned char cArray[3][5];

    foo(array, cArray);
}

我可以硬连线 unsigned char,如下所示

unsigned char cArray[3][5] = {"a105b", "c258e", "ff587"};

不,这在 C++ 中无效。您必须在 C++ 中创建内部数组 [6]

unsigned char cArray[3][6] = {"a105b", "c258e", "ff587"};

【讨论】:

    【解决方案2】:

    在代码中可能如下所示:

    #include <array>
    #include <algorithm>
    #include <cstring>
    #include <string>
    
    template<typename to_type, size_t buf_size, size_t number_of_strings>
    void to_array(const std::array<std::string, number_of_strings>& input, 
                  to_type (&output)[number_of_strings][buf_size])   
    {
        for (std::size_t n = 0; n < number_of_strings; ++n)
        {
            const auto input_str = input[n].c_str();
            // for input string include trailing 0 on input so add one to length
            const auto copy_len = std::min(input[n].length()+1, buf_size); 
            std::memcpy(output[n], input_str, copy_len);
        }
    }
    
    int main()
    {
        std::array<std::string, 3> input_array{ "a105b", "c258e", "ff587" };
        unsigned char c_array[3][6];
        to_array<unsigned char, 6>(input_array, c_array);
    
        return 0;
    }
    

    它再次向我展示了 'c' 样式的数组不适合使用。 您不能从函数中返回它们(就像使用 std::array 一样)。 所以你也必须将输出数组作为参数传递给转换函数。

    【讨论】:

    • 哦,我一定是在睡觉;)代码已调整
    【解决方案3】:

    您不能分配给普通数组。您不能为普通数组定义自己的赋值运算符,因为 C++ 不允许赋值运算符重载,除非作为类的非静态成员函数。

    一种解决方法可能是为移位运算符定义重载,并为输入流提供类似的语法。

    template <unsigned N>
    void operator >> (std::string s, unsigned char (&a)[N]) {
        auto n = (s.size() < N) ? s.size() + 1 : N;
        std::copy_n(s.c_str(), n, a);
    }
    
    /*...*/
        unsigned char cArray[3][5];
        array[0] >> cArray[0];
        array[1] >> cArray[1];
        array[2] >> cArray[2];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-21
      • 2013-10-30
      • 1970-01-01
      • 2017-09-17
      • 2018-09-13
      • 2011-06-09
      相关资源
      最近更新 更多