C ++十六进制到十进制转换器 [英] C++ hexadecimal to decimal converter

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

问题描述

问候,



我目前遇到了一个问题,即使在调试时我似乎也听不懂。我试图创建一个十六进制到十进制转换器程序,但我遇到了一个我无法解决的问题。每当我尝试输入8F或更高的转换为十进制时,它最终会给出16F的结果(也就是最大范围:18446744 .... 615)



这是代码:

Greetings,

I currently have run into an issue that I seem to not understand even while debugging. I am trying to create a hexadecimal to decimal converter program, but I've run into a problem that I can't fix. Every time I try to input 8Fs or higher to convert into decimal, it ends up giving 16Fs' result (aka the max range:18446744....615)

This is the code:

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int powerFunc(int x, int y)
{
	unsigned long long int result = 1;
	int i;

	for (i = 0; i < y; i++)
	{
		result *= x;
	}

	return result;
}

int hexToDec(string hexaNumber)
{
	int i;
	int power = hexaNumber.length() - 1;
	int checkLength = hexaNumber.length();
	unsigned long long int decimalResult = 0;

	//Convert Hexa to Decimal for Number;

	for (i = 0; i < checkLength; i++)
	{
		if (int(hexaNumber[i]) >= '0' && int(hexaNumber[i]) <= '9') {				// check if FirstHexaNumber 0 to 9

			decimalResult += ((int(hexaNumber[i])) - 48) * powerFunc(16, power);	//formula to convert hexadecimal into decimal, int(FirstHexaNumber[i]) is used to convert hexa into a number
		}
		else if (int(hexaNumber[i]) >= 'A' && int(hexaNumber[i]) <= 'F')			// check if FirstHexaNumber is A to F
		{
			decimalResult += ((int(hexaNumber[i])) - 55)*powerFunc(16, power);
		}
		else if (int(hexaNumber[i]) >= 'a' && int(hexaNumber[i]) <= 'f')			// check if FirstHexaNumber is a to f
		{
			decimalResult += ((int(hexaNumber[i])) - 87)*powerFunc(16, power);
		}
		power--;			//power-- since it starts from "HexaNumber.length - 1". Power should decrease as assignment of power goes down
	}

	return decimalResult;
}

int main()
{
	unsigned long long int result;
	unsigned long long int dec1;
	unsigned long long int dec2;
	unsigned long long int totalDec;
	string hexa1;

	string FirstHexaNumber;

	getline(cin, FirstHexaNumber);

	dec1 = hexToDec(FirstHexaNumber);

	cout << dec1 << endl;
	

	return 0;
}





我的尝试:



我试过调试问题,电源功能工作正常,它设法得到应该得到的结果。在这种情况下,当我输入FFFFFFFF时,我得到的功率是268435456,这是正确的。



然而,当它到达转换部分时,它会混乱起来。由于某种原因,在公式的for循环的第一次运行中,(int(F) - 55)* 16 ^ 8 =它最终给出18446744 .... 615。我该怎么做才能解决这个问题?在此先感谢。



What I have tried:

I have tried debugging the issue, the power function works fine, it manages to get the result it should get. In this case, when I input "FFFFFFFF", the power I get is 268435456, which is correct.

However, when it gets to the converting part, it messes up. For some reason, on the first run on the for loop for the formula, (int(F) - 55) * 16^8 = it ends up giving 18446744....615. What can I do to solve this issue? Thanks in advance.

推荐答案

powerFunc返回一个int:

powerFunc returns an int:
int powerFunc(int x, int y)
{
   ...
}

和16 ^ 8是4,294,967,296或十六进制0x100000000 - 它不适合32位整数!

and 16^8 is 4,294,967,296 or in hex 0x100000000 - which doesn't fit in a 32 bit integer!


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

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