View Single Post
 Mentor 1Apr2010,11:38 After inputting the number from the user, set a variable V to zero. Loop over each digit from right to left and multiply V by 10, add the character code from the input and subtract the character code for '0'. This is because '0'-'9' are consecutive in the characterset, so '5'-'0'=5. For the hex output, loop over V from left to right and get the byte value. Divide by 16 to get the upper 4 bits. If the resulting value is between 0 and 9 then add '0' and display the result. If the value is between 10 and 15 then add 'a' and subtract 10, and display the result. Then get the byte value again and AND it with 15 to get the lower 4 bits, and display the value in the same way as the upper 4. For the binary output, loop over V from left to right and get the byte value Y. Initialise a bitmask variable B to 128 and AND this with Y. If the result is zero, display '0', otherwise display '1'. Divide B by 2 and repeat for the 6th binary digit. Keep dividing B by 2 and displaying bits until B=0. Then go onto the next byte value, reinitialise B to 128 and continue until you're done. Easy!