View Full Version : C++ help
tominator
12-08-2004, 07:55 PM
does anyone know how to write a function in C++ that converts a hexadecimal number into a decimal number?
thanks
morcos
12-08-2004, 08:53 PM
hey tominator.
im not sure of a program in C but i can explain to you how to convert if this is what you need to know.
lets say you get 0x245 (hex)
divide 0x245 by 1000 (decimal) and store that as digit 1
divide the remainder of that by 100 (decimal) and store that as digit 2
divide the remainder of that by 10... and so on
if you need to convet a bigger hex file start with a higher value to divide by.
hope that helps man
tominator
12-08-2004, 09:50 PM
alright what i did in my program so far is this...its suppose to convert this 3 digit hexadecimal number to a decimal number. but i think its wrong....its not printing the decimal number...just 61039383...all that junk, can anyone help. if i enter a 34A, it should conver that to a decimal. i think there is a formula.
#include <iostream>
int digit ( int );
int main()
{
int number;
digit( number );
return 0;
}
int digit( int number )
{
std::cout << "Enter a 3 Digit Hexadecimal Number please.\n";
std::cin >> number;
if ( number < 1 || number > 999 )
{
std::cout << "Invalid Entry! Enter a 3 Digit Hexadecimal Number please.\n";
std::cin >> number;
}
int one = 1000 % number;
int two = 100 % one;
int three = 10 % two;
std::cout << " Decimal value is:" << one << two << three << std::endl;
return 0;
}
morcos
12-09-2004, 08:28 AM
//change this part here to take up to FFF?
if ( number < 1 || number > 999 )
/*
* hey man i think i found why you arent seeing the right value on the screen.
* -if you want to get a hex number from the user it will have to be
* something other than int because int doesnt take letters for values.
* -maybe you want to convert each character to binary first... thats easy
* enough, then convert the binary to decimal.
* -you are in the range of 0x001 to 0x999. maybe you want 0x001 to 0xFFF?
*/
//hex to decimal:
for ( i=0 ; i < 3 ; i++ )
{
switch ( szHex[i] )
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
//then dont change the number
binary1 = szHex[i];
case 'A':
//and so on... give that a try man.
vBulletin® v3.7.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.