| Function: | |
CnvtBase |
| | |
| Category: | |
Math |
| | |
| Action: | |
Converts numbers from any base (2-36) to any base (2-36). i.e. base 2=binary, 8=octal, 10=decimal, 16=hex, etc.
|
| | |
| Syntax | |
CNVTBASE ( number, [oldbase], [newbase] )
|
| | |
| Parameters: |
|
| Name |
Type
|
Optional
|
Description
|
| number |
String
|
Required
|
The number (passed as a string) to convert from.
|
| oldbase |
String
|
Required
|
The base that number is in. (If not supplied or out of range, cnvtbase will try to "guess" whether it is binary, octal, decimal, hexadecimal, or some base between 17 and 36).
|
| newbase |
String
|
Required
|
The base to which the number will be converted. If not supplied or out of range, decimal (base 10) will be used.
|
|
| | |
| Remarks: | |
Use this function to convert among several bases. I.e. when a number must be represented in decimal, hex, and binary formats. I originally wrote this as a training exercise when learning a different programming language, but it has proven helpful in many cases, so I've converted the logic to KiXtart. The key information contained is the generalized algorithm for converting to base 10 and to other bases containing alpha characters. It also demonstrates the need for knowing which data type you are working with at any given time! KiXtart is pretty flexible, but it can't read minds.
"Number" is a string representing a number, base 2 - 36. Oldbase and newbase may be any number 2 - 36. If oldbase is not supplied, the function will "guess" the old base as base 2, 8, 10, 16, or 17-36 depending on the highest value "digit" contained in the string. If the string is prepended with a hyphen "-" (i.e. negative number), the hyphen will be preserved in the return string.
IMPORTANT: The function returns the converted number as a STRING in order to correctly handle bases greater than 10.
One final note: The function will return a "0" if the supplied number string contains any non-alphanumeric characters besides a leading hyphen (-). This means the function could even be used to test whether a string is alphanumeric.
Unusual behaviors: This function will produce unreliable results for numbers that evaluate to a base 10 number outside the range of -2,147,483,647 to 2,147,483,647. Also: If newbase is not supplied or is out of range (< 2 or > 36), the function will convert to decimal (base 10).
|
| | |
| Returns: | |
The function returns a string representing the number in the new base specified (or base 10 if none specified).
|
| | |
| Dependencies: | | None.
|
| | |
| Examples: |
|
|
| | |
| Source: |
|
|
| | |
| Created By: | |
W.M. Hinsch (New Mexico Mark) |
|