简体   繁体   English

C ++:如何将字符串向量元素转换为无符号字符?

[英]C++: how to convert string vector elements into unsigned char?

I have a string vector and a self declared type of unsigned char: 我有一个字符串向量和一个自声明类型的无符号字符:

std::vector<std::string> result;
typedef unsigned char BYTE;
BYTE data[11];

I have assigned values to result. 我已经分配了结果。 Now I want to copy the elements of result into data : 现在,我想将result的元素复制到data

for ( int i=0; i<11; i++)
{
    data[i] = result[i];
}

But it gives out error, saying No suitable conversion function from std::string to BYTE exists . 但是它给出了错误,说不No suitable conversion function from std::string to BYTE exists

How can I handle this? 我该如何处理?

Update: 更新:

First, thank you all for your precious comments and answers. 首先,感谢大家的宝贵评论和答复。 In my code, my result is of six elements like these values: 在我的代码中,我的result是包含以下六个值的元素:

0x30
0x31
0x32
0x33
0x34
0x35

I tried as Raxvan's answer, but at the end, data only captures the first value of those string elements, that is, all zeros. 我尝试作为Raxvan的答案,但最后, data仅捕获那些字符串元素的第一个值,即全零。 What I actually want to do is to let data have the same values as result , but of BYTE type, so that it is similar to assigning values like this: 我实际上想要做的是让data具有与result相同的值,但是具有BYTE类型,因此它类似于分配这样的值:

BYTE data[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35 } // this line works correctly

string encapsulates an array of char and the compiler doesn't know how to convert that into one char. string封装了一个char数组,编译器不知道如何将其转换为一个char。 If you want to take the first character from the strings then you can do this: 如果要从字符串中获取第一个字符,则可以执行以下操作:

for ( int i=0,j = 0; i<11 && j < result.size(); j++)
{
    if(result[j].size() != 0) //safety first.
        data[i++] = result[j][0];
}

You're confusing your types. 您混淆了您的类型。

The data is an array of BYTE (so basically able to hold 11 characters ), but result is an array of string (meaning it can hold multiple strings , each of those holding multiple characters). data是一个BYTE数组(因此基本上可以容纳11个字符 ),但是result是一个string数组(这意味着它可以容纳多个字符串 ,每个字符串都包含多个字符)。

So depending on what you actually need you can do two things: 因此,根据您的实际需求,您可以做两件事:

If you need all strings to be converted into BYTE arrays, then create an array of arrays of BYTE: 如果您需要将所有字符串都转换为BYTE数组,则创建一个BYTE数组数组:

// adapt sizes to your needs
size_t const MaxStrSize = 11; // allows to hold 10 characters (+ 0 delimiter)
size_t const MaxStrCount = 11;
BYTE data[MaxStrCount][MaxStrSize];

for (int j=0; j<MaxStrCount && j<result.size(); ++j)
{
    for ( int i=0; i<MaxStrSize && i<result[i].length(); i++)
    {
        data[j][i] = result[j][i];
    }
}

Otherwise, if you only want to access one of the strings contained in the vector (eg the first one): See Raxvan's answer . 否则,如果您只想访问向量中包含的字符串之一(例如第一个):请参见Raxvan的答案

Its long since I have written some CPP code, so kindly excuse for the syntax errors. 自从我写了一些CPP代码以来,它很久以来就为语法错误辩解。 I think what OP is trying to achieve is convert the 2D array to a single comma separated string. 我认为OP试图实现的是将2D数组转换为单个逗号分隔的字符串。

Here is my sample code: 这是我的示例代码:

 char [] DATA = "";

        for ( int j = 0;j < result.count(); j++)
        { 
          if(j < result.count()-2)
          result = strcat(result, ",")
          DATA =       strcat(DATA, result);
        }

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

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