如何将字符串转换为 uint32_t

p.l*_*uck 7 c++ io types casting file-read

我有一个程序,它逐行读取文件的内容,将每一行存储到字符串向量中,然后打印向量的内容。

\n\n

将文件数据读入字符串向量后,我尝试将每一行stringuint32. 文件的每一行都由32-bit数字组成。输入数据文件的示例(input_file.dat)

\n\n
31401402\n67662718\n74620743\n54690001\n14530874\n13263047\n84662943\n09732679\n13839873\n
Run Code Online (Sandbox Code Playgroud)\n\n

我想将这些字符串中的每一个转换为uint32_t, 对于我编写的另一个程序,该程序将这些数字转换为 ASCII 格式(该程序需要用于uint32_t转换)。

\n\n

到目前为止我的计划:

\n\n
#include <iostream>\n#include <fstream>\n#include <string>\n#include <vector>\n\n/*\n * Program to iterate through all lines in file and put them in given vector\n *\n */\nbool getFileContent(std::string fileName, std::vector<std::string> & vecOfStrs)\n{\n\n    // Open the File\n    std::ifstream in(fileName.c_str());\n\n    // Check if object is valid\n    if(!in)\n    {\n        std::cerr << "Cannot open the File : "<<fileName<<std::endl;\n        return false;\n    }\n\n    std::string str;\n    // Read the next line from File untill it reaches the end.\n    while (std::getline(in, str))\n    {\n        // Line contains string of length > 0 then save it in vector\n        if(str.size() > 0)\n            vecOfStrs.push_back(str);\n    }\n    //Close The File\n    in.close();\n    return true;\n}\n\n\nint main()\n{\n    //Store contents in this vector of strings\n    std::vector<std::string> vecOfStr;\n\n    // Get the contents of file in a vector\n    bool result = getFileContent("input_file.dat", vecOfStr);\n\n    // If above result is true\n    if(result)\n    {\n        // Iterate through the vector contents\n        for(std::string & line : vecOfStr)\n        {\n            //std::cout<<line<<std::endl;      Ignore this print line, it is for debugging only to show that the vector has been correctly filled\n\n            //For each line, convert from string to uint32_t\n            std::vector<std::uint32_t>new_variable\n            new_variable.assign(vecOfStr.begin(), vecOfStr.end());\n\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

在我上面的尝试中,在for注释“//迭代向量内容”下方的循环中,我尝试使用该函数将每一行从 a 转换string为。我在研究我的问题时这样做了,并从另一个SO问题中发现了这个函数:最快的方法将 std::vector 转换为另一个 std::vector(作者:Vlad,于 11 年 10 月 26 日 7:36 回答)。当我尝试运行此代码时,我收到以下消息:uint32_tvector::assignerror

\n\n
error: cannot convert \xe2\x80\x98std::__cxx11::basic_string<char>\xe2\x80\x99 to \xe2\x80\x98unsigned int\xe2\x80\x99 in initialization\n
Run Code Online (Sandbox Code Playgroud)\n\n

总结一下:

\n\n

如何逐行读取文件内容,并将每一行转换为数据类型uint32_t?我确保每行都等于 32 位。

\n

joh*_*ohn 8

像这样

std::vector<std::uint32_t> new_variable;
for (string str : vecOfStr)
    new_variable.push_back(static_cast<uint32_t>(std::stoul(str)));
Run Code Online (Sandbox Code Playgroud)

stoul函数将字符串转换为整数。严格来说,它转换unsigned long为非uint32_t. 但是 的所有合法值都uint32_t可以用 an 表示unsigned long,并且强制转换会转换回uint32_t您想要的值。

还有其他方法(std::transform例如使用),但我发现显式循环更简单。

上面的代码没有错误检查。如果有必要的话,像 artm 的答案这样的东西是更好的。

顺便说一句,您的尝试失败了,因为a和 a之间没有隐式转换。当存在隐式转换时,您尝试的方法就会起作用。stringuint32_t

  • 需要注意一点:`for (string str : vecOfStr)` -&gt; `for (string const&amp; str : vecOfStr)`以避免复制。 (4认同)
  • `uint32_t &gt; int32_t`。你会遇到溢出问题。 (2认同)
  • @NathanOliver 警告,我讨厌警告 (2认同)

归档时间:

查看次数:

39259 次

最近记录:

5 年,6 月 前