将std :: string转换为uint32_t

问题描述 投票:-2回答:2

我有一个如下字符串:

std::string strl="ffffffffffffffffffffffffffffffffffffffffffff";

我想将它转换为uint32_t变量,如下所示:

uint32_t val = std::stoul(strl, nullptr, 16);

上面的操作给出了“SIGABRT”信号并给出错误:

terminate called after throwing an instance of 'std::out_of_range'
what():  stoul.

要解决此问题需要做哪些更改,或者使用uint32_t数据类型来存储字符串是不可能的。

c++ string uint32-t
2个回答
2
投票

uint32_t只能存储0xffffffff,因为它是32位unsigned类型,因此无法使用该数据类型存储您的字符串。

对于您提供的字符串,您需要一个大整数库来解析它。

Boost有一个很好的,甚至包括像typedefuint1024_ts,所以它使用起来非常简单。

http://www.boost.org/doc/libs/1_58_0/libs/multiprecision/doc/html/index.html


1
投票

如果您确实想将数字存储在uint32_t中,则需要对其进行验证。

我会接近这样的事情:

#include <string>
#include <cstdint>
#include <stdexcept>
#include <iostream>

auto parse_hex_uint32(std::string const& input) -> std::uint32_t
try
{
    std::size_t read_len = 0;

    auto initial_result = std::stoull(input, &read_len, 16);
    if (read_len != input.size())
    {
        throw std::runtime_error("invalid input: " + input);
    }
    if (initial_result > std::numeric_limits<std::uint32_t>::max())
    {
        throw std::out_of_range("number too large: " + std::to_string(initial_result));
    }

    return std::uint32_t(initial_result);
}
catch(...)
{
    std::throw_with_nested(std::runtime_error("failed to parse " + input + " as hex uint32"));
}

void print_exception(const std::exception& e, int level =  0)
{
    std::cerr << std::string(level, ' ') << "exception: " << e.what() << '\n';
    try {
        std::rethrow_if_nested(e);
    } catch(const std::exception& e) {
        print_exception(e, level+1);
    } catch(...) {}
}

int main()
{
    using namespace std::literals;
    auto input = "ffffffffffffffff"s;
    try
    {
        std::cout << parse_hex_uint32(input) << std::endl;
        return 0;
    }
    catch(std::exception& e)
    {
        print_exception(e);
        return 100;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.