Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从字符串到Ice::ByteSeq的转换 我想问一下C++中的ICE问题。我的一个方法需要传入一个Ice::ByteSeq。我想从一个字符串构建这个ByteSeq。这种转换如何可能_C++_Ice - Fatal编程技术网

从字符串到Ice::ByteSeq的转换 我想问一下C++中的ICE问题。我的一个方法需要传入一个Ice::ByteSeq。我想从一个字符串构建这个ByteSeq。这种转换如何可能

从字符串到Ice::ByteSeq的转换 我想问一下C++中的ICE问题。我的一个方法需要传入一个Ice::ByteSeq。我想从一个字符串构建这个ByteSeq。这种转换如何可能,c++,ice,C++,Ice,我尝试了下面的选项 Ice::ByteSeq("bytes") // Invalid conversion to unsigned int Ice::ByteSeq((byte*)"bytes") // Invalid conversion from byte* to unsigned int (Ice::ByteSeq)"bytes" // Invalid conversion from const char& to unsigned int (Ice::B

我尝试了下面的选项

Ice::ByteSeq("bytes")        // Invalid conversion to unsigned int
Ice::ByteSeq((byte*)"bytes") // Invalid conversion from byte* to unsigned int
(Ice::ByteSeq)"bytes"        // Invalid conversion from const char& to unsigned int
(Ice::ByteSeq)(unsigned int)atoi("bytes") // Blank (obviously, why did I try this?)
我怎样才能做到这一点

编辑

“bytes”
是一个占位符值。实际上,我的字符串是非数字文本信息。

你就快到了

Ice::ByteSeq((unsigned int)atoi("bytes"));
应该这样做

假设您的Ice::ByteSeq有一个接受无符号int的构造函数

要将其分解,它基本上是

int num = atoi("12345"); // num = (int) 12345 
unsigned int num2 = (unsigned int)num;  // num2 = (unsigned int) 12345
Ice::ByteSeq(num2);
看一下,
ByteSeq
vector
的别名。您可以用通常的方式从
std::string
初始化它

std::string s = "whatever";
Ice::ByteSeq bs(s.begin(), s.end());
或者从一个字符串文本中添加一点修饰,例如

template <size_t N>
Ice::ByteSeq byteseq_from_literal(char (&s)[N]) {
    return Ice::ByteSeq(s, s+N-1); // assuming you don't want to include the terminator
}

Ice::ByteSeq bs = byteseq_from_literal("whatever");
模板
Ice::ByteSeq ByteSeq_from_literal(char&s)[N]){
return Ice::ByteSeq(s,s+N-1);//假设您不想包含终止符
}
Ice::ByteSeq bs=ByteSeq_from_literal(“无论什么”);

如果
Ice::ByteSeq
只是一个字节向量,则可以通过执行以下操作将字符串转换为字节向量:

std::string str = "Hello World";
std::vector<char> bytes(str.begin(), str.end());


希望这有帮助:)

Ice::ByteSeq是如何定义的?您需要传递一个将转换为字节数组的字符串吗?@Raxvan我希望我能弄明白。我现在有点讨厌ZeroC。GMasucci我需要一个字符串来Ice::ByteSeq转换。这几乎可以工作,问题是
atoi
命令使我的字符串无效,因为它是字符而不是数字。字符串实际包含什么,以及
Ice::ByteSeq
构造函数希望收到什么?它是文本。“<代码>”命令“”,我不知道什么是“代码> ICE::ByTeeq 预期,它不在ZROOC的文档中。您必须在某处有一个头文件。如果您在VisualC++中,在编辑器中右击ByTeeq,然后转到定义,并找到构造函数是如何定义当前使用GEDIT的。所以不,不使用Visual C++。我试图追查它的源头,但冰中有奇怪的内含物。嗯。。。这管用!我不知道为什么找不到
.h
文件。到目前为止,我对冰的印象不太好。谢谢你的帮助!
std::vector<char> bytes(str.begin(), str.end());
std::vector<unsigned char> bytes(str.begin(), str.end());
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    std::string str = "Hello World";
    std::vector<unsigned char> bytes(str.begin(), str.end());
    cout << str << endl; 

    for(int i=0; i < bytes.size(); i++)
        std::cout << bytes[i] << '\n';
   return 0;
}