Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 12-08-2004, 06:55 PM   #1
Member (4 bit)
 
Join Date: Dec 2004
Posts: 8
C++ help

does anyone know how to write a function in C++ that converts a hexadecimal number into a decimal number?
thanks
tominator is offline   Reply With Quote
Old 12-08-2004, 07:53 PM   #2
Member (4 bit)
 
Join Date: Sep 2003
Posts: 14
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
morcos is offline   Reply With Quote
Old 12-08-2004, 08:50 PM   #3
Member (4 bit)
 
Join Date: Dec 2004
Posts: 8
worng conversion?

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

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;
}
tominator is offline   Reply With Quote
Old 12-09-2004, 07:28 AM   #4
Member (4 bit)
 
Join Date: Sep 2003
Posts: 14
//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.
morcos is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:13 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2