Help with pseudocode

08/31/2012 00:49 EatMyChidori#1
Can someone help me with writing a pseudocode for converting hex to decimal?
08/31/2012 02:52 bloodx#2
maybe this?
Code:
    int x;
    std::cin >> std::hex >> x;
    std::cout << x << std::endl;

    return 0;
08/31/2012 03:26 EatMyChidori#3
Could you kindly explain what those things even mean? LOL. My class for programming just started and i dont know much
08/31/2012 12:37 .SkyneT.#4
Quote:
Originally Posted by EatMyChidori View Post
Could you kindly explain what those things even mean? LOL. My class for programming just started and i dont know much
Code:
std::cin >> std::hex >> x;
Saves an hexadecimal value to x.

Code:
std::cout << x << std::endl;
or
Code:
std::cout << std::dec <<  x << std::endl;
Shows the value in the decimal system.
08/31/2012 14:49 MrSm!th#5
That's no pseudo code.
09/01/2012 22:29 Cowmangler#6
Here's some C-inspired "pseudocode"
Quote:
mul = 1;
for (i = hex.length; i>= 0; i--, mul*16)
{
result += hex[i] * mul;
}
I hope that's what you were looking for
09/01/2012 23:52 MrSm!th#7
That wouldn't do the job, since hex characters may include letters (A-F) that cannot be multiplied with 16.
09/02/2012 01:12 Ensky#8
Well, it's bad pseudo-code and only a "quick'n'dirty" solution, but maybe it helps... :-)

Quote:
long f(character *a, integer length)
{
integer i;
long value = 0;

for(i=0;i<length;i++)
if(a[i] <= 57) //look at ASCII Table, if you don't understand this :-)
value += (a[i]-48) * (1 leftshift (4*(length-1-i)));
else
value += (a[i]-55) * (1 leftshift (4*(length-1-i)));
return value;
}
09/02/2012 02:00 Jadd#9
C++ solution was posted above, however this is the C solution:

Code:
int nInteger = 0;
scanf_s( "%x", &nInteger );
printf_s( "%i", nInteger );
09/02/2012 03:29 MrSm!th#10
Quote:
Originally Posted by Jadd View Post
C++ solution was posted above, however this is the C solution:

Code:
int nInteger = 0;
scanf_s( "%x", &nInteger );
printf_s( "%i", nInteger );
Well, I'm clearly sure, that he needs to write the algorithm himself instead of using libraries.
09/03/2012 16:56 kissein#11
Algo works from right to left.

Divide number by 16. Keep the quotient for next iteration, the remainder is the figure you are looking for. It will ovbviously be between 0 and 15, convert that to 0..F
As long as quotient is >= 16, repeat.
09/04/2012 06:34 Ensky#12
Quote:
Originally Posted by kissein View Post
Algo works from right to left.

Divide number by 16. Keep the quotient for next iteration, the remainder is the figure you are looking for. It will ovbviously be between 0 and 15, convert that to 0..F
As long as quotient is >= 16, repeat.

he want's to convert hex to decimal, not decimal to hex :-)
09/04/2012 11:06 kissein#13
Quote:
Originally Posted by Ensky View Post
he want's to convert hex to decimal, not decimal to hex :-)
ups sorry, my fault.

-Get the last digit of the hex number, call this digit the currentDigit.
-Make a variable, let's call it power. Set the value to 0.
-Multiply the current digit with (16^power), store the result.
-Increment power by 1.
-Set the the currentDigit to the previous digit of the hex number.
-Repeat from step 3 until all digits have been multiplied.
-Sum the result of step 3 to get the answer number.

Example:

Convert the number 35432 HEXADECIMAL to DECIMAL

2x(16^0) + 3x(16^1) + 4x(16^2) + 5x(16^3) + 3x(16^4) =
2 + 3x16 + 4*256 + 5*4096 + 3*65536 =
2 + 48 + 1024 + 20480 + 196608 =
218162