425,563 Members | 1,058 Online Need help? Post your question and get tips & solutions from a community of 425,563 IT Pros & Developers. It's quick & easy.

# C++: Converting a 3 digit hexadecimal number to decimal help?

 P: 4 What would be the easiest way of going about converting a 3 digit hexadecimal number to decimal, I think that a switch statement is out of the question due to multiple digits. Would it be easiest just to assign A - F numerical values and just do the arithmetic? And just to clarify, all of the hexadecimals are base 16. And can somebody please clarify what the value of 3A7 is? My impression is that it is (3 * 1) + (10 * 16) + (7 * 1) = 170 ? Sep 19 '07 #1
7 Replies

 Expert 100+ P: 463 Actually, 3A7, is 3 * 16**2 + 10* 16**1 + 7 * 1, where 16**N is 16 raised to the power of N. Add all of these up and you have the decimal number you're looking for. Sep 19 '07 #2

 P: 4 Actually, 3A7, is 3 * 16**2 + 10* 16**1 + 7 * 1, where 16**N is 16 raised to the power of N. Add all of these up and you have the decimal number you're looking for. Okay, so Each digit is assigned a power starting from the last being power 0 and ascending from there. Sep 19 '07 #3

 P: 4 Now that i've got the arithmetic part, what would be the easiest way to write this in C++? Sep 21 '07 #4

 Expert Mod 2.5K+ P: 4,677 Now that i've got the arithmetic part, what would be the easiest way to write this in C++? Develop your algorithm for an 'any case' scenario. This is usually done by doing 3 or 4 by hand and figuring out how to abstract it for every case. That will give you an algorithm, which you can change into pseudocode, which you can then code. Sep 21 '07 #5

 Expert Mod 5K+ P: 9,197 This is C++ right?? Use a string stream: Expand|Select|Wrap|Line Numbers int main() {     stringstream ss;     ss << 0X1A;     int result;     ss >> result;     cout << result << endl;       //Or use a string     ss.flush();     string str("0x1A");     ss << str;     ss >> result;     cout << result << endl;   }   The mathematical stuff has been done to death. Sep 22 '07 #6

 100+ P: 134 this is probably the long way to do it, but it uses simple functions seperate the string [ex. '8A3'] with strcpy/strtok, making string1, string2, string3 if (string1=="0"){int1==0;} if (string1=="1"){int1==1;} .... if (string1=="A"){int1==10;} if (string1=="B"){int1==11;} ... then do same with string2 and string3 then mulitply int2*16 and int3*56 then add int1+int2+int3 Sep 23 '07 #7

 100+ P: 121 If you are as lazy as me, google for "C++ hex to dec convert" and you would see this: http://www.daniweb.com/code/snippet470.html A typical answer with good sample code. Actually for the algorithm, I would try this(In ANSI C, this is my concern): First, put user input into an char array. Second, use a loop to: read array[i]. do the math convert from hex to dec //switch cases where char == 'A', ... store the result into dec[i] //an int array When you finish the loop, you should get the fully int array dec[] Then you use another loop to work out the result Return it to the user. This is the newbie's approach, so just fundamental data types and basic knowledges are used. Sep 23 '07 #8

### This discussion thread is closed

Replies have been disabled for this discussion. 