【问题标题】:conversion from `const char*' to `byte'从 `const char*' 到 `byte' 的转换
【发布时间】:2014-12-11 21:42:21
【问题描述】:

我无法将 const char 转换为 byte。我正在使用 ifstream 读取文件,它将内容作为字符串提供,然后我使用 c_str(); 将字符串转换为 const char;然后尝试将其插入字节数组以进行数据包发送。我是 C++ 新手,不明白我必须如何将 char 转换为 byte,需要你们的帮助。这是我的一段代码,请给我一些建议

byte buf[42];

const char* fname = path.c_str();

ifstream inFile;
inFile.open(fname);//open the input file

stringstream strStream;
strStream << inFile.rdbuf();//read the file
string str = strStream.str();//str holds the content of the file

vector<string> result = explode(str,',');

for (size_t i = 0; i < result.size(); i++) {
    buf[i] = result[i].c_str(); // Here is Error
    cout << "\"" << result[i] << "\"" << endl;
}

system("pause");

这是我从文件中获取的数据:(0x68,0x32,0x01,0x7B,0x01,0x1F,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00)

【问题讨论】:

  • 这个byte 类型是如何定义的?它不是标准 c++ 的一部分...
  • 您正在尝试将指向字符串的指针存储在字节数组中。 1 个字节不能容纳整个字符串。我不确定您要做什么。
  • byte 不是 C++11 的标准类型。你是说int8_t 吗?
  • 我正在从 php 代码创建十六进制值,然后我将其保存到文件中,然后我使用 C++ 代码打开此文件并希望将此十六进制值分配给字节数组,是的,我知道我做错了,但我不知道我必须如何正确地做=/
  • 您阅读的文件是否包含文本“0x68 ...”,即字母“0”后跟字母“x”等,就像您在编辑器中看到的那样? (这会使文件大小>60。)或者它是否包含大约 15 个字节,第一个字节的值为 0x68?

标签: c++ char byte converter


【解决方案1】:

您正在尝试将字符串(许多字符)分配给单个字节。它不适合。 尝试类似

在循环开始之前添加:

size_t bufpos = 0;

然后在循环内

const string & str = resulti[i];
for (size_t strpos = 0; strpos < str.size() && bufpos < sizeof(buf); ++strpos)
{
   buf[bufpos++] = str[strpos];
}

【讨论】:

  • 是的,但爆炸字符串 (0x68,0x32,0x01,0x7B,0x01,0x1F,0x00 ...) 作为 1 个十六进制值,需要像这样放置数组 [0]=0x68,我想要与此字节类似的字节数组 buf[42] = { 0x68,0x32,0x01,0x7B,0x01,0x1F,0x00,0x00,0x00, 0x02,0x00,0x00,0x00,0x00,0x00,0x03, 0x12,0x00,0x57, 0x12,0x00,0x65,0x12,0x00,0x6C,0x12,0x00,0x63,0x12,0x00,0x6F,0x12,0x00,0x6D,0x12,0x00,0x65,0x00,0x00,0x00, 0x86,0x03 }
  • 当您不断改变目标并添加新元素时,您的问题很难回答。 array[0] 是从哪里来的?我建议你退后一步,定义你想要实现的目标,而不是用你所采取的方法来关注低层次的问题。
  • 我的意思是字节 buf[0] 作为数组 [0] 但无论如何,如果你知道,请回答我,如果我有字符串 try = "0x23" 是否可以将此字符串转换为字节值并插入它变成字节 buf[1] = { 0x23 },(对不起我的英语)
  • @DTDest 所以你真正想要的是获取一个包含数字的十六进制表示的字符串并将该数字提取到byte(不管是什么)?
  • 是的,我想要这个,取 0x23 这个字符串值将其转换为十六进制 0x23 并将其放入字节数组中。如果你知道,请告诉我我必须做什么=/
【解决方案2】:

我自己做的,现在我将解释解决方案。所以我想要 String(0x68,0x32,0x03,0x22 etc..) 变量拆分每个“,”,然后在将其作为 16 位十六进制值输入字节数组后将其转换为十六进制值。

char buf[42]; // Define Packet

const char* fname = path.c_str(); // File Location


ifstream inFile; //
inFile.open(fname);//open the input file

stringstream strStream;
strStream << inFile.rdbuf();//read the file
string str = strStream.str();//str holds the content of the file



vector<string> result = explode(str,','); // Explode Per comma


for (size_t i = 0; i < result.size(); i++) { // loop for every exploded value

unsigned int x;   
std::stringstream ss;
ss << std::hex <<  result[i];    // Convert String Into Integer value 
ss >> x;
buf[i] = x;

 printf(&buf[i],"%04x",x); //Convert integer value back to 16 bit hex value and store into array


    }



  system("pause");

感谢大家重播。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多