C++:将十六进制转换为十进制

17

我正在寻找一种将 hex(十六进制) 转换为 dec(十进制) 的简单方法。我发现了一个简单的方法,就像这样:

int k = 0x265;
cout << k << endl;

但是我不能输入 265,有没有办法让它像这样工作:

输入:265

输出:613

有没有办法做到这一点?

注意:我已经尝试过:

int k = 0x, b;
cin >> b;
cout << k + b << endl;

但它不起作用。


这不是很清楚。0x265在十进制中是613。你期望的是什么? - Oliver Charlesworth
@Esailija 我觉得它是将整数转换为十六进制。 - zeulb
1
@Oli Charlesworth 我想输入“265”并输出“613”。 - zeulb
2
可能重复:https://dev59.com/HkbRa4cB1Zd3GeqP5ftQ - Chris
2
https://dev59.com/gkfRa4cB1Zd3GeqP83H8 - Chris
11个回答

26
#include <iostream>
#include <iomanip>
#include <sstream>

int main()
{
    int x, y;
    std::stringstream stream;

    std::cin >> x;
    stream << x;
    stream >> std::hex >> y;
    std::cout << y;

    return 0;
}

15
为什么需要中间的stringstream,而不直接使用cin >> hex >> y - Oliver Charlesworth
2
你不需要 #include <sstream> 吗? - xinthose

16

使用std::hex操纵器:

#include <iostream>
#include <iomanip>

int main()
{
    int x;
    std::cin >> std::hex >> x;
    std::cout << x << std::endl;

    return 0;
}

1
有没有不使用 input 的方法?因为我想将原始输入用于其他用途。 - zeulb
1
@zeulb,不太确定你的意思。但是你可以通过std::cin >> std::dec;cin恢复为使用十进制。 - hmjd
我是说我想在两个不同的变量上同时使用十六进制和十进制。 - zeulb
1
输入是十六进制还是十进制并不影响int变量的内部表示,那又怎样?请表达得更清楚一些。您是否想在同一语句中将流中的值存储到两个变量中? - πάντα ῥεῖ
比如我使用了'cin >> hex >> k;',然后我输入了265,现在k的值是613。我想要的是将265存储在变量p中,而613保留在变量k中。对于我的糟糕的英语表示抱歉。 - zeulb
你的问题中没有变量 p。这篇文章回答了你所发布的问题 :( - Mooing Duck

6
很好,C语言的做法可能是这样的…
#include <stdlib.h>
#include <stdio.h>

int main()
{
        int n;
        scanf("%d", &n);
        printf("%X", n);

        exit(0);
}

6

以下是一种使用字符串并将其转换为十进制的 ASCII 表格的解决方案:

#include <iostream>
#include <string>
#include "math.h"
using namespace std;
unsigned long hex2dec(string hex)
{
    unsigned long result = 0;
    for (int i=0; i<hex.length(); i++) {
        if (hex[i]>=48 && hex[i]<=57)
        {
            result += (hex[i]-48)*pow(16,hex.length()-i-1);
        } else if (hex[i]>=65 && hex[i]<=70) {
            result += (hex[i]-55)*pow(16,hex.length( )-i-1);
        } else if (hex[i]>=97 && hex[i]<=102) {
            result += (hex[i]-87)*pow(16,hex.length()-i-1);
        }
    }
    return result;
}

int main(int argc, const char * argv[]) {
    string hex_str;
    cin >> hex_str;
    cout << hex2dec(hex_str) << endl;
    return 0;
}

1
我使用这个:

template <typename T>
bool fromHex(const std::string& hexValue, T& result)
{
    std::stringstream ss;
    ss << std::hex << hexValue;
    ss >> result;

    return !ss.fail();
}

1
    std::cout << "Enter decimal number: " ;
    std::cin >> input ;

    std::cout << "0x" << std::hex << input << '\n' ;

如果您添加一个可以是布尔型、浮点型或整数型的输入,它将在int主函数调用中返回...
通过函数模板,基于参数类型,C生成单独的函数来适当地处理每种类型的调用。所有函数模板定义都以关键字template开头,后跟用尖括号<和>括起来的参数。使用单个形式参数T来测试数据类型。
考虑以下程序,用户被要求输入一个整数和一个浮点数,每个输入都使用平方函数来确定平方值。通过函数模板,基于参数类型,C生成单独的函数来适当地处理每种类型的调用。所有函数模板定义都以关键字template开头,后跟用尖括号<和>括起来的参数。使用单个形式参数T来测试数据类型。
#include <iostream>
 using namespace std;
template <class T>      // function template
T square(T);    /* returns a value of type T and accepts                  type T     (int or float or whatever) */
  void main()
{
int x, y;
float w, z;
cout << "Enter a integer:  ";
cin >> x;
y = square(x);
cout << "The square of that number is:  " << y << endl;
cout << "Enter a float:  ";
cin >> w;
z = square(w);
cout << "The square of that number is:  " << z << endl;
}

template <class T>      // function template
T square(T u) //accepts a parameter u of type T (int or float)
{
return u * u;
}

Here is the output:

Enter a integer:  5
The square of that number is:  25
Enter a float:  5.3
The square of that number is:  28.09

0

只使用:

cout << dec << 0x;


0
如果您有一个十六进制字符串,您也可以使用以下方法将其转换为十进制。
int base = 16;
std::string numberString = "0xa";
char *end; 
long long int number;

number = strtoll(numberString.c_str(), &end, base);

0

我认为这更清晰,而且也能够处理你的异常。

#include <iostream>
using namespace std;
using ll = long long;
int main ()
{
    ll int x;
    cin >> hex >> x;
    cout << x;
}

0

std::stoi, stol, stoul, stoull 可以将数字转换为不同的进制

long long hex2dec(std::string hex)
{
    std::string::size_type sz = 0;
    try
    {
        hex = "0x"s + hex;
        return std::stoll(hex, &sz, 16);
    }
    catch (...)
    {
        return 0;
    }
}

如果你需要返回字符串,可以使用类似的方法

std::string hex2decstr(std::string hex)
{
    std::string::size_type sz = 0;
    try
    {
        hex = "0x"s + hex;
        return std::to_string(std::stoull(hex, &sz, 16));
    }
    catch (...)
    {
        return "";
    }
}

使用方法:

std::string converted = hex2decstr("16B564");

你是否尝试过使用std::from_charsstd::to_chars - MatG

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接