convert_base()
Convert a number from one base to another.
Syntax
Parameters
Parameter | Definition |
---|---|
number | number that is converted from from_base to to_base*Valid Values (in base 10)* |
from_base | base from which number is converted*Valid Values* |
to_base | base to which number is converted*Valid Values* |
sign | optional flag to specify if number is signed If this value is omitted, the default is signed. A non-zero value indicates that the value is unsigned. |
Description
The convert_base() function converts a numeric value, number, from one base,from_base, to another base, to_base.
The convert_base () function returns an empty string and sets the following PSL errno values under the following conditions:
errno Value | Description |
---|---|
68 | E_PSL_EDOM |
69 | E_PSL_ERANGE |
If any part of number is invalid for from_base, convert_base() stops the conversion and returns any part of number that it was able to read. For example, the command convert_base("78", 8, 10); returns 7 because 8 is invalid for base 8. If you enter text for number, you might find it harder to notice when you have entered an invalid number for from_base. For example, the commandconvert_base("az", 16, 10); returns 10 because, in base 16, "a" equals 10 and "z" is invalid.
Example
The following PSL example converts a base 10 number supplied by the user to a base specified by the user. This function uses the convert_base() function for the base conversion and the response() function to create a GUI for the user to enter the desired values:
string=response("Base Conversion",-1,
["h=100","w=100","b=Patrol","e=1","o=Accept","c=Cancel
","N=0","A=0","B=0"],
[R_COLUMN, 4],
[R_LABEL_CENTER, "Enter a base 10 number,\n
and select a base to convert it to:"],
[4],[R_LABEL, " "],
[R_TEXT_FIELD_LABEL, "Number", "\n", "e=1"],
[R_ROW, 2],
[R_LABEL, " "],
[R_CLICKER, "Base", 2,36,2]
);
# Obtain status, number, and desired base from response function
return
stat=trim(nthlinef(string,"1"),"\n");
numb=trim(nthlinef(string,"2"),"\n");
base=trim(nthlinef(string,"3"),"\n");
# Verify the return status if the dialog was CANCELLED (0), exit
if (stat==0)
{
exit;
} # Verify number is numeric and within range if not display error
message
elsif (isnumber(numb)==0 || numb>2147483647 || numb<-2147483647)
{
response("Error",-1,
["h=100","w=100","b=Patrol","e=1","o=OK","c=Cancel
","N=1","A=0","B=0"],
[R_COLUMN, 3],[4],[R_LABEL, " "],
[R_LABEL_CENTER, "The value entered\n\n"."(".numb.")"."\n\n
is not a valid number, or it is out of range!"]
);
exit;
} # If input is acceptable, compute new value and display output
dialog
else
{
# Use the convert_base() function to convert numb from base 10 to
base
new=convert_base(numb,"10",base);
response("Conversion",-1,
["h=100","w=100","b=Patrol","e=1","o=OK","c=Cancel
","N=1","A=0","B=0"],
[R_COLUMN, 3],[4],[R_LABEL, " "],
[R_LABEL_CENTER, numb." converted to base ".base." = ".new]
);
}