## FUN

Posted by Circuit Negma on April 10, 2010

Created By: Hussein Nosair

I am trying to build a power base converter that would convert any power based number to a decimal (10 base) number.

However, I am not completely done. This is what I have so far. By the way I am using Matlab.

` 1: %-------------------------------------------------------------------------`

` 2: % Created By: Hussein Nosair`

` 3: % Date : 4/10/2010`

` 4: % Description:`

` 5: % The following algorithm convert any power base number to a 10 base`

6: % system, keeping in mind that this module accepts only numbers and not

7: % alphabets. Trying to keep this code as simple as possible, however, a

` 8: % future work will includes the use of alphabets.`

` 9: %`

` 10: % Result = nth_digit * base^index + (n-1)_digit * base^(index-1) + ....`

` 11: %`

` 12: % Example:`

` 13: % 32564 base 8`

` 14: % n = 5 digits`

` 15: % base = 8`

` 16: % index = nth digit position with in the number to be converted`

` 17: % Result = 15029`

` 18: % 32654 base 8 ---> 15029 base 10`

` 19: %-------------------------------------------------------------------------`

` 20: `

21: function y = base_n(input, base)

` 22: `

` 23: % Clear Window`

` 24: clc`

` 25: `

` 26: % Define and Initialize variables`

` 27: x = input;`

` 28: i = 0;`

` 29: n = 10;`

` 30: y = 0;`

` 31: `

` 32: % Prepare`

33: fprintf(' ___________________________________________________________________\n');

34: fprintf('| |\n');

35: fprintf('| base: %d |\n', base);

36: fprintf('| Number: %d |\n', input);

37: fprintf('|-------------------------------------------------------------------|\n');

38: fprintf('| index digit nth digit New x Result|\n');

39: fprintf('|-------------------------------------------------------------------|\n');

` 40: `

` 41: % Power Conversion Algorithm`

42: while 1

` 43: % Determine the n digit from the given number:`

` 44: z = mod(x,n);`

` 45: r = (z * 10)/n;`

` 46: `

47: % Calculate a number for the next nth digit extraction

` 48: x = x - z;`

` 49: `

` 50: % Perform the conversion`

` 51: y = y + (r * base^i);`

` 52: `

` 53: % Print out the result on screen`

54: fprintf('| %3.5d %3.5d %3.5d %3.5d %3.5d |\n', i, z, r, x, y);

` 55: `

56: % Check if conversion is over

57: if x == 0

` 58: % Display the final results.`

59: fprintf('|-------------------------------------------------------------------|\n');

60: fprintf('| Number: %d |\n', input);

61: fprintf('| base: %d |\n', base);

62: fprintf('| n: %d |\n', i+1);

63: fprintf('| Result: %d |\n', y);

64: fprintf('|___________________________________________________________________|\n');

65: break;

` 66: end`

` 67: `

68: fprintf('|-------------------------------------------------------------------|\n');

` 69: i = i + 1;`

` 70: n = n * 10; `

` 71: end`

72: fprintf('\n');

## Leave a Reply