如何将字符串转换为 uint32_t
How to convert a string to uint32_t
我有一个程序,它逐行读取文件的内容,将每一行存储到一个字符串向量中,然后打印向量的内容。
将文件数据读入字符串向量后,我试图将每一行从 string
转换为 uint32
。文件的每一行都包含 32-bit
个数字。输入数据文件示例 (input_file.dat)
:
31401402
67662718
74620743
54690001
14530874
13263047
84662943
09732679
13839873
我想将这些字符串中的每一个都转换为 uint32_t
,用于我编写的将这些数字转换为 ASCII 格式的不同程序(程序 要求 uint32_t
用于转换)。
到目前为止我的程序:
#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
循环中,“// Iterate through the vector contents”,我试图将每一行从 string
转换为 uint32_t
使用 vector::assign
函数。我在研究我的问题时这样做了,并从另一个 SO 问题中找到了这个函数:fastest way to convert a std::vector to another std::vector(作者:Vlad,2011 年 10 月 26 日在 7:36 回答)。当我尝试 运行 此代码时,我收到以下 error
消息:
error: cannot convert ‘std::__cxx11::basic_string<char>’ to ‘unsigned int’ in initialization
总结:
如何逐行读取文件内容,并将每一行转换为数据类型uint32_t
?我确保每行等于 32 位。
像这样
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
表示,并且强制转换会转换回您想要的 uint32_t
。
还有其他方法(例如使用 std::transform
),但我发现显式循环更简单。
上面的代码没有错误检查。如果有必要,最好是 artm 的回答。
顺便说一下,您的尝试失败了,因为 string
和 uint32_t
之间没有 隐式转换 。当存在隐式转换时,您尝试的方法将起作用。
使用 stringstream
进行格式化,您需要包含 <stringstream>
另外:
将 new_variable
移到循环外,否则生成的 vector
将无用
不需要传递vecOfStrs
作为参考。按价值 return vector
会更好。
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 );
}
我有一个程序,它逐行读取文件的内容,将每一行存储到一个字符串向量中,然后打印向量的内容。
将文件数据读入字符串向量后,我试图将每一行从 string
转换为 uint32
。文件的每一行都包含 32-bit
个数字。输入数据文件示例 (input_file.dat)
:
31401402
67662718
74620743
54690001
14530874
13263047
84662943
09732679
13839873
我想将这些字符串中的每一个都转换为 uint32_t
,用于我编写的将这些数字转换为 ASCII 格式的不同程序(程序 要求 uint32_t
用于转换)。
到目前为止我的程序:
#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
循环中,“// Iterate through the vector contents”,我试图将每一行从 string
转换为 uint32_t
使用 vector::assign
函数。我在研究我的问题时这样做了,并从另一个 SO 问题中找到了这个函数:fastest way to convert a std::vector to another std::vector(作者:Vlad,2011 年 10 月 26 日在 7:36 回答)。当我尝试 运行 此代码时,我收到以下 error
消息:
error: cannot convert ‘std::__cxx11::basic_string<char>’ to ‘unsigned int’ in initialization
总结:
如何逐行读取文件内容,并将每一行转换为数据类型uint32_t
?我确保每行等于 32 位。
像这样
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
表示,并且强制转换会转换回您想要的 uint32_t
。
还有其他方法(例如使用 std::transform
),但我发现显式循环更简单。
上面的代码没有错误检查。如果有必要,最好是 artm 的回答。
顺便说一下,您的尝试失败了,因为 string
和 uint32_t
之间没有 隐式转换 。当存在隐式转换时,您尝试的方法将起作用。
使用 stringstream
进行格式化,您需要包含 <stringstream>
另外:
将
new_variable
移到循环外,否则生成的vector
将无用不需要传递
vecOfStrs
作为参考。按价值 returnvector
会更好。
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 );
}