How to convert a string to uint32_t

p.luck picture p.luck · Jul 29, 2019 · Viewed 11.3k times · Source

I have a program which reads the contents of a file line by line, stores each line into a vector of strings, and then prints the contents of the vector.

After reading the file data into the vector of strings, I am attempting to convert each line from string into uint32. Each line of the file consists of 32-bit numbers. An example of the input data file (input_file.dat):

31401402
67662718
74620743
54690001
14530874
13263047
84662943
09732679
13839873

I would like to convert each of those strings into uint32_t, for a different program I have written which converts these numbers into ASCII format (the program requires that uint32_t is used for conversion).

My program so far:

#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());

        }
    }
}

In my above attempt, in the for loop underneath the comment which reads "// Iterate through the vector contents", I have attempted to convert each line from a string to uint32_t using the vector::assign function. I did this when researching my problem, and found this function from another SO question: fastest way to convert a std::vector to another std::vector (author: Vlad, answered Oct 26 '11 at 7:36). When I try to run this code, I receive the following error message:

error: cannot convert ‘std::__cxx11::basic_string<char>’ to ‘unsigned int’ in initialization

TO SUMMARISE:

How can I read the contents of a file line by line, and convert each line into the data type uint32_t? I have ensured that each line is equal to 32-bits.

Answer

john picture john · Jul 29, 2019

Like this

std::vector<std::uint32_t> new_variable;
for (string str : vecOfStr)
    new_variable.push_back(static_cast<uint32_t>(std::stoul(str)));

The stoul function converts to string to the integer. Strictly it converts to an unsigned long not a uint32_t. But all legal values of uint32_t can be represented by an unsigned long and a cast converts back to the uint32_t that you want.

There's other ways (using std::transform for instance), but I find an explicit loop to be simpler.

There's no error checking in the code above. If that's necessary something like artm's answer is preferable.

BTW your attempt failed because there's no implicit conversion between a string and a uint32_t. When there is an implicit conversion then what you tried would work.