C++ C++;:十六进制到十进制的转换

C++ C++;:十六进制到十进制的转换,c++,algorithm,hex,decimal,C++,Algorithm,Hex,Decimal,我正在寻找一种将hex(十六进制)轻松转换为dec(十进制)的方法。我找到了一个简单的方法,比如: int k = 0x265; cout << k << endl; 而且它不工作。使用操纵器: #include <iostream> #include <iomanip> int main() { int x; std::cin >> std::hex >> x; std::cout <&l

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

int k = 0x265;
cout << k << endl;
而且它不工作。

使用操纵器:

#include <iostream>
#include <iomanip>

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

    return 0;
}
#包括
#包括
int main()
{
int x;
标准:cin>>标准:十六进制>>x;
std::cout
#包括
#包括
#包括
int main()
{
int x,y;
std::stringstream;
标准:cin>>x;
流>标准::十六进制>>y;

std::cout嗯,C方式可能类似于

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

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

        exit(0);
}
#包括
#包括
int main()
{
int n;
scanf(“%d”和“&n”);
printf(“%X”,n);
出口(0);
}

这是一个使用字符串并使用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;
}
#包括
#包括
#包括“math.h”
使用名称空间std;
无符号长十六进制(字符串十六进制)
{
无符号长结果=0;
对于(int i=0;i=48&&hex[i]=65&&hex[i]=97&&hex[i]>hex\u str;
我能用这个吗

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

    return !ss.fail();
}
模板
bool fromHex(const std::string和hexValue、T和result)
{
std::stringstream-ss;
ss结果;
return!ss.fail();
}
std::cout>输入;

std::cout这也应该有效

#include <ctype.h>
#include <string.h>

template<typename T = unsigned int>
T Hex2Int(const char* const Hexstr, bool* Overflow)
{
    if (!Hexstr)
        return false;
    if (Overflow)
        *Overflow = false;

    auto between = [](char val, char c1, char c2) { return val >= c1 && val <= c2; };
    size_t len = strlen(Hexstr);
    T result = 0;

    for (size_t i = 0, offset = sizeof(T) << 3; i < len && (int)offset > 0; i++)
    {
        if (between(Hexstr[i], '0', '9'))
            result = result << 4 ^ Hexstr[i] - '0';
        else if (between(tolower(Hexstr[i]), 'a', 'f'))
            result = result << 4 ^ tolower(Hexstr[i]) - ('a' - 10); // Remove the decimal part;
        offset -= 4;
    }
    if (((len + ((len % 2) != 0)) << 2) > (sizeof(T) << 3) && Overflow)
        *Overflow = true;
    return result;
}
#包括
#包括
模板
T Hex2Int(常量字符*常量Hexstr,布尔*溢出)
{
如果(!hextr)
返回false;
如果(溢出)
*溢出=错误;
自动介于=[](字符值、字符c1、字符c2){return val>=c1&&val仅使用:


cout这不清楚。
0x265
是十进制的613。你在期待什么?@Esailija我想它将int转换为十六进制。可能重复:这里:可能重复反正不使用输入就可以这样做?因为我想用原始输入做些什么else@zeulb,我不知道你的确切意思。但是你可以回复
cin
使用十进制by
std::cin>>std::dec;
我的意思是我想在两个不同的变量上同时使用十六进制和十进制输入方式(十六进制或十进制)对int变量的内部表示没有任何影响,那么什么呢?请更清楚地表达你自己。你想把流中的值存储到同一语句中的两个变量吗?就像我使用'cin>>hex>>k;',然后我输入265,现在k值是613。我想把265存储在变量p上,613保留在变量k上。对不起,我的英语不好。为什么是中间字符串流呢?为什么不是暗示
cin>>hex>>y
?您不需要包含吗?
template <typename T>
bool fromHex(const std::string& hexValue, T& result)
{
    std::stringstream ss;
    ss << std::hex << hexValue;
    ss >> result;

    return !ss.fail();
}
    std::cout << "Enter decimal number: " ;
    std::cin >> input ;

    std::cout << "0x" << std::hex << input << '\n' ;
#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
#include <ctype.h>
#include <string.h>

template<typename T = unsigned int>
T Hex2Int(const char* const Hexstr, bool* Overflow)
{
    if (!Hexstr)
        return false;
    if (Overflow)
        *Overflow = false;

    auto between = [](char val, char c1, char c2) { return val >= c1 && val <= c2; };
    size_t len = strlen(Hexstr);
    T result = 0;

    for (size_t i = 0, offset = sizeof(T) << 3; i < len && (int)offset > 0; i++)
    {
        if (between(Hexstr[i], '0', '9'))
            result = result << 4 ^ Hexstr[i] - '0';
        else if (between(tolower(Hexstr[i]), 'a', 'f'))
            result = result << 4 ^ tolower(Hexstr[i]) - ('a' - 10); // Remove the decimal part;
        offset -= 4;
    }
    if (((len + ((len % 2) != 0)) << 2) > (sizeof(T) << 3) && Overflow)
        *Overflow = true;
    return result;
}
auto result = Hex2Int("C0ffee", NULL);
auto result2 = Hex2Int<long>("DeadC0ffe", NULL);