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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11031159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:46:02  来源:igfitidea点击:

Converting Hexadecimal to Decimal

c++algorithmhexdecimal

提问by zeulb

I'm looking for a way to convert hex(hexadecimal)to dec(decimal)easily. I found an easy way to do this like :

我正在寻找一种轻松将hex(十六进制)转换为dec(十进制)的方法。我找到了一个简单的方法来做到这一点:

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

But with that I can't input 265. Is there anyway for it to work like that:

但是我无法输入265. 无论如何,它是否可以这样工作:

Input: 265

输入: 265

Output: 613

输出: 613

Is there anyway to do that ?

有没有办法做到这一点?

Note:I've tried:

注意:我试过:

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

and it doesn't work.

它不起作用。

回答by smichak

#include <iostream>
#include <iomanip>

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

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

    return 0;
}

回答by hmjd

Use std::hexmanipulator:

使用std::hex机械手:

#include <iostream>
#include <iomanip>

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

    return 0;
}

回答by Christos

Here is a solution using strings and converting it to decimal with ASCII tables:

这是一个使用字符串并使用 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;
}

回答by stefanl

Well, the C way might be something like ...

好吧,C 方式可能类似于...

#include <stdlib.h>
#include <stdio.h>

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

        exit(0);
}

回答by 101dolmations

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

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

if your adding a input that can be a boolean or float or int it will be passed back in the int main function call...

如果您添加的输入可以是布尔值或浮点数或整数,它将在 int 主函数调用中传回...

With function templates, based on argument types, C generates separate functions to handle each type of call appropriately. All function template definitions begin with the keyword template followed by arguments enclosed in angle brackets < and >. A single formal parameter T is used for the type of data to be tested.

使用函数模板,基于参数类型,C 生成单独的函数来适当地处理每种类型的调用。所有函数模板定义都以关键字 template 开头,后跟括在尖括号 < 和 > 中的参数。单个形式参数 T 用于要测试的数据类型。

Consider the following program where the user is asked to enter an integer and then a float, each uses the square function to determine the square. With function templates, based on argument types, C generates separate functions to handle each type of call appropriately. All function template definitions begin with the keyword template followed by arguments enclosed in angle brackets < and >. A single formal parameter T is used for the type of data to be tested.

考虑以下程序,其中要求用户输入一个整数,然后输入一个浮点数,每个程序都使用 square 函数来确定平方。使用函数模板,基于参数类型,C 生成单独的函数来适当地处理每种类型的调用。所有函数模板定义都以关键字 template 开头,后跟括在尖括号 < 和 > 中的参数。单个形式参数 T 用于要测试的数据类型。

Consider the following program where the user is asked to enter an integer and then a float, each uses the square function to determine the square.

考虑以下程序,其中要求用户输入一个整数,然后输入一个浮点数,每个程序都使用 square 函数来确定平方。

#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

回答by dimon4eg

I use this:

我用这个:

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

    return !ss.fail();
}