C ++:将十六进制转换为十进制 [英] C++: Converting Hexadecimal to Decimal

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

问题描述

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

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

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

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

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

输入:265

输出:613

反正有这样做吗?

注意:我已经尝试过:

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

它不起作用.

推荐答案

#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;
}

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

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