首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将字符串转换为uint32_t

如何将字符串转换为uint32_t
EN

Stack Overflow用户
提问于 2019-07-29 12:24:22
回答 2查看 27K关注 0票数 5

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

在将文件数据读入字符串向量之后,我将尝试将string中的每一行转换为uint32。文件的每一行都由32-bit编号组成。输入数据文件(input_file.dat)的示例

代码语言:javascript
运行
复制
31401402
67662718
74620743
54690001
14530874
13263047
84662943
09732679
13839873

我想将每个字符串转换为uint32_t,因为我编写了一个不同的程序,它将这些数字转换为ASCII格式(程序需要使用来进行转换)。

到目前为止我的计划:

代码语言:javascript
运行
复制
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

/*
 * Program to iterate through all lines in file and put them in given vector
 *
 */
bool getFileContent(std::string fileName, std::vector<std::string> & vecOfStrs)
{

    // Open the File
    std::ifstream in(fileName.c_str());

    // Check if object is valid
    if(!in)
    {
        std::cerr << "Cannot open the File : "<<fileName<<std::endl;
        return false;
    }

    std::string str;
    // Read the next line from File untill it reaches the end.
    while (std::getline(in, str))
    {
        // Line contains string of length > 0 then save it in vector
        if(str.size() > 0)
            vecOfStrs.push_back(str);
    }
    //Close The File
    in.close();
    return true;
}


int main()
{
    //Store contents in this vector of strings
    std::vector<std::string> vecOfStr;

    // Get the contents of file in a vector
    bool result = getFileContent("input_file.dat", vecOfStr);

    // If above result is true
    if(result)
    {
        // Iterate through the vector contents
        for(std::string & line : vecOfStr)
        {
            //std::cout<<line<<std::endl;      Ignore this print line, it is for debugging only to show that the vector has been correctly filled

            //For each line, convert from string to uint32_t
            std::vector<std::uint32_t>new_variable
            new_variable.assign(vecOfStr.begin(), vecOfStr.end());

        }
    }
}

在上面的尝试中,在注释下面的for循环中,我尝试使用vector::assign函数将每一行从string转换为uint32_t。我在研究我的问题时就这样做了,并从另一个这样的问题中找到了这个函数:fastest way to convert a std::vector to another std::vector (作者:弗拉德,10月26日7点36分回答)。当我尝试运行这段代码时,我收到以下error消息:

代码语言:javascript
运行
复制
error: cannot convert ‘std::__cxx11::basic_string<char>’ to ‘unsigned int’ in initialization

概述:

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-29 12:28:12

像这样

代码语言:javascript
运行
复制
std::vector<std::uint32_t> new_variable;
for (string str : vecOfStr)
    new_variable.push_back(static_cast<uint32_t>(std::stoul(str)));

stoul函数将字符串转换为整数。严格地说,它将转换为unsigned long而不是uint32_t。但是,uint32_t的所有合法值都可以由unsigned long表示,cast转换回所需的uint32_t

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

在上面的代码中没有错误检查。如果这是必要的,类似于阿特姆的答案是可取的。

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

票数 8
EN

Stack Overflow用户

发布于 2019-07-29 12:36:13

使用stringstream进行格式化,您需要包括<stringstream>

另外:

  • new_variable移出循环,否则生成的vector将不起作用
  • 不需要传递vecOfStrs作为参考。最好只按值返回vector
代码语言:javascript
运行
复制
        std::vector<std::uint32_t> new_variable;

        for(std::string & line : vecOfStr)
        {
            //For each line, convert from string to uint32_t

            std::stringstream ss(line);
            uint32_t val;
            if( !( ss >> val ) )
            {
                throw std::runtime_error( "failed to convert to uint32" );
            }
            
            std::cout << "val = " << val << '\n';   
            new_variable.push_back( val );
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57253837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档