C++ 将十六进制字符串转换为有符号整数 [英] C++ convert hex string to signed integer

查看:68
本文介绍了C++ 将十六进制字符串转换为有符号整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 C++ 中将十六进制字符串转换为 32 位有符号整数.

I want to convert a hex string to a 32 bit signed integer in C++.

因此,例如,我有十六进制字符串ffeffeffe".它的二进制表示是 11111111111111101111111111111110.它的有符号整数表示是:-65538.

So, for example, I have the hex string "fffefffe". The binary representation of this is 11111111111111101111111111111110. The signed integer representation of this is: -65538.

我如何在 C++ 中进行这种转换?这也需要适用于非负数.例如十六进制字符串0000000A",二进制为00000000000000000000000000001010,十进制为10.

How do I do this conversion in C++? This also needs to work for non-negative numbers. For example, the hex string "0000000A", which is 00000000000000000000000000001010 in binary, and 10 in decimal.

推荐答案

use std::stringstream

unsigned int x;   
std::stringstream ss;
ss << std::hex << "fffefffe";
ss >> x;

以下示例生成 -65538 作为其结果:

the following example produces -65538 as its result:

#include <sstream>
#include <iostream>

int main() {
    unsigned int x;   
    std::stringstream ss;
    ss << std::hex << "fffefffe";
    ss >> x;
    // output it as a signed type
    std::cout << static_cast<int>(x) << std::endl;
}

在新的 C++11 标准中,有一些新的实用函数可供您使用!具体来说,有一系列字符串到数字"函数(http://en.cppreference.com/w/cpp/string/basic_string/stolhttp://en.cppreference.com/w/cpp/string/basic_string/stoul).这些本质上是 C 的字符串到数字转换函数的薄包装,但知道如何处理 std::string

In the new C++11 standard, there are a few new utility functions which you can make use of! specifically, there is a family of "string to number" functions (http://en.cppreference.com/w/cpp/string/basic_string/stol and http://en.cppreference.com/w/cpp/string/basic_string/stoul). These are essentially thin wrappers around C's string to number conversion functions, but know how to deal with a std::string

因此,对于较新的代码,最简单的答案可能如下所示:

So, the simplest answer for newer code would probably look like this:

std::string s = "0xfffefffe";
unsigned int x = std::stoul(s, nullptr, 16);

<小时>

注意:以下是我的原始答案,正如编辑所说,这不是一个完整的答案.对于功能性解决方案,将代码粘贴在行上方 :-).


NOTE: Below is my original answer, which as the edit says is not a complete answer. For a functional solution, stick the code above the line :-).

看来,由于 lexical_cast<> 被定义为具有流转换语义.可悲的是,流不理解0x"符号.所以 boost::lexical_cast 和我的手卷都不能很好地处理十六进制字符串.上面手动将输入流设置为十六进制的解决方案可以很好地处理它.

It appears that since lexical_cast<> is defined to have stream conversion semantics. Sadly, streams don't understand the "0x" notation. So both the boost::lexical_cast and my hand rolled one don't deal well with hex strings. The above solution which manually sets the input stream to hex will handle it just fine.

Boost 有一些东西可以做到这一点好吧,它也有一些很好的错误检查功能.你可以这样使用它:

Boost has some stuff to do this as well, which has some nice error checking capabilities as well. You can use it like this:

try {
    unsigned int x = lexical_cast<int>("0x0badc0de");
} catch(bad_lexical_cast &) {
    // whatever you want to do...
}

如果您不想使用 boost,这里有一个不进行错误检查的精简版词法转换:

If you don't feel like using boost, here's a light version of lexical cast which does no error checking:

template<typename T2, typename T1>
inline T2 lexical_cast(const T1 &in) {
    T2 out;
    std::stringstream ss;
    ss << in;
    ss >> out;
    return out;
}

你可以这样使用:

// though this needs the 0x prefix so it knows it is hex
unsigned int x = lexical_cast<unsigned int>("0xdeadbeef"); 

这篇关于C++ 将十六进制字符串转换为有符号整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆