Re: Hex decimal conversion in Delphi
[ Related Articles and Replies ] [ DelphiLand FAQ ] [ Delphi Tutorials ]
Posted by webmaster Guido on December 13, 2002 at 14:04:40:
In Reply to: Hex decimal conversion in Delphi posted by Rohan on December 12, 2002 at 14:37:59:
: i need some basic code for a number converting Delphi function to convert a number in any base (specified by user) to a number in any base (again specified by user). 
That's possible, but the words "basic code" are somewhat misleading ;) since it depends from what you want: only binary, octal, decimal and hexadecimal, or ALL number bases? (such as base 24 and base 256 and so on...) And further more: only integer ("whole") numbers, or also "real" numbers with a decimal fraction? For positive integer inputs, with a number base of maximum 10, it's easy: 1. Check the input string for validity (see if all the digits are valid in the number system used). 2. Convert the digits of the input string to integers, working from right to left and put these intermediate results in an array. 3. Multiply all the elements of the array with powers of the base number: base to the power 0, 1, 2... For example, for binary this would be 1, 2, 4, 8... For octal, it would be 1, 8, 64,... 4. The result is the sum of the elements of the array. But if you want to go beyond that, this is in fact a full blown project that you are asking for :) Just some ideas: 1. You have to agree on a standard notation system for representing digits that are higher then 10, if the number base is higher then 10. For example, hexadecimal uses A to F for values from 10 to 15, a base of 20 would require A to J for representing 10 to 19, and so on. 2. The error checking code might be more complicated then the conversions, because the characters that are allowed in the input string depend from the number base. For base 2 (binary) you allow only 0 and 1, for octal accept digits from 0 to 7, decimal is 0 to 9, hexadecimal is 0 to F... 3. Using 0 to 9 and the uppercase alphabetic characters A to Z limits the maximal number base to 36. Also using lowercase characters allows you to go up to base 62. If you want to go higher, you need to design some very different system for representing the numbers. 4. Once that you go over base 17, it will be difficult to read for humans. What is I, is it 1 or the 9th letter of the alfabet? O, is this zero or O? Not to mention the confusion between 0, O and o...
Related Articles and Replies:
[ Related Articles and Replies ] [ DelphiLand FAQ ] [ Delphi Tutorials ]

