Qore Programming Language Reference Manual  1.9.1
Type Conversion Functions
Collaboration diagram for Type Conversion Functions:

Modules

 String Type Constants
 

Functions

auto Qore::auto_cast (auto val)
 Returns complex types converted to the most specific types possible using internal type folding. More...
 
binary Qore::binary ()
 Always returns an empty binary object (of zero length) More...
 
binary Qore::binary (null x)
 Always returns an empty binary object (of zero length) More...
 
binary Qore::binary (softstring str)
 Returns a binary data type of the string passed; data types other than string will first be converted to a string and then returned as binary data. More...
 
binary Qore::binary (binary bin)
 Always returns the same binary object passed. More...
 
string Qore::binary_to_string (binary b, *string encoding)
 Returns a string created from the binary data passed, taking an optional second argument giving the string encoding; if no second argument is passed then the default character encoding is assumed. More...
 
bool Qore::boolean (any arg)
 Converts the argument to a boolean value. More...
 
float Qore::float (softfloat f)
 Converts the argument to a floating-point (float) value. More...
 
float Qore::float ()
 Always returns 0.0. More...
 
hash< auto > Qore::hash (object obj)
 Returns a hash of an object's members. More...
 
hash< auto > Qore::hash (list< auto > l)
 Returns a hash by taking even numbered list elements (starting with 0) and converting them to strings for the hash keys, and the odd numbered elements following the keys as the key value. More...
 
hash< auto > Qore::hash (list< softstring > keys, list< auto > values)
 Returns a hash by taking the first list as a list of keys, and the second list as a list of values. More...
 
hash Qore::hash (hash h)
 Returns the same hash with all complex types stripped. More...
 
hash< auto > Qore::hash ()
 Always returns the same hash passed. More...
 
int Qore::int (string str, int base=10)
 Converts the string to an integer value with respect to the base. More...
 
int Qore::int (softint i)
 Converts the argument to an integer value. More...
 
int Qore::int ()
 Always returns 0. More...
 
list< auto > Qore::list (...)
 Returns a list of the arguments passed at the top level. More...
 
number Qore::number (softnumber n)
 Converts the argument to a number value. More...
 
number Qore::number ()
 Always returns 0.0. More...
 
string Qore::string (softstring str, *string enc)
 Converts the argument to a string. More...
 
string Qore::string ()
 Always returns an empty string. More...
 
string Qore::type (auto arg)
 Returns a string giving the data type of the argument passed; see String Type Constants for the values returned by this function. More...
 
string Qore::typename (auto arg)
 Returns a string giving the data type of the argument passed; see String Type Constants for the values returned by this function. More...
 

Detailed Description

Function Documentation

◆ auto_cast()

auto Qore::auto_cast ( auto  val)

Returns complex types converted to the most specific types possible using internal type folding.

Code Flags:
CONSTANT
Example:
list<auto> tmp;
tmp += 1;
tmp += 2;
list<int> int_list = auto_cast(tmp);
auto auto_cast(auto val)
Returns complex types converted to the most specific types possible using internal type folding.
Parameters
valthe value to convert
Returns
a copy of the input value with complex types converted to the most specific types possible using internal type folding
Since
Qore 0.9.3

◆ binary() [1/4]

binary Qore::binary ( )

Always returns an empty binary object (of zero length)

Code Flags:
CONSTANT

The binary() function is used for type conversions, therefore this variant is not tagged with NOOP

See also

◆ binary() [2/4]

binary Qore::binary ( binary  bin)

Always returns the same binary object passed.

Code Flags:
CONSTANT

The binary() function is used for type conversions, therefore this variant is not tagged with NOOP

◆ binary() [3/4]

binary Qore::binary ( null  x)

Always returns an empty binary object (of zero length)

Code Flags:
CONSTANT

The binary() function is used for type conversions, therefore this variant is not tagged with NOOP

See also
binary(softstring)

◆ binary() [4/4]

binary Qore::binary ( softstring  str)

Returns a binary data type of the string passed; data types other than string will first be converted to a string and then returned as binary data.

This function is useful if, for example, a string type actually contains binary data; using this function will ensure that all data in the string (even if it contains embedded nulls) is maintained in the binary object (Qore strings must normally be terminated by a single null, so some Qore string operations do not work on binary data with embedded nulls).

Code Flags:
CONSTANT
Example:
binary b = binary(str);
binary binary()
Always returns an empty binary object (of zero length)
See also
<string>::toBinary()

◆ binary_to_string()

string Qore::binary_to_string ( binary  b,
*string  encoding 
)

Returns a string created from the binary data passed, taking an optional second argument giving the string encoding; if no second argument is passed then the default character encoding is assumed.

Code Flags:
CONSTANT
Parameters
bthe binary object to convert directly to a string
encodingthe character encoding tag for the string return value; if not present, the default character encoding is assumed
Returns
a string created from the binary data passed
Example:
string str = binary_to_string(b, "iso-8859-1");
string binary_to_string(binary b, *string encoding)
Returns a string created from the binary data passed, taking an optional second argument giving the s...

◆ boolean()

bool Qore::boolean ( any  arg)

Converts the argument to a boolean value.

Code Flags:
CONSTANT
Parameters
argthe argument to convert to a boolean
Returns
the boolean value corresponding to the argument
Example:
bool b = boolean(1); # returns True
bool boolean(any arg)
Converts the argument to a boolean value.
Note
this function behaves differently when %strict-bool-eval is set

◆ float() [1/2]

float Qore::float ( )

Always returns 0.0.

Code Flags:
CONSTANT

The float() function is used for type conversions, therefore this variant is not tagged with NOOP

See also
float(softfloat)

◆ float() [2/2]

float Qore::float ( softfloat  f)

Converts the argument to a floating-point (float) value.

Code Flags:
CONSTANT
Parameters
fthe argument to convert to a float
Returns
the float value corresponding to the argument
Example:
float i = float("3.1415");
float float(softfloat f)
Converts the argument to a floating-point (float) value.

◆ hash() [1/5]

hash<auto> Qore::hash ( )

Always returns the same hash passed.

Code Flags:
CONSTANT

The hash() function is used for type conversions, therefore this variant is not tagged with NOOP

◆ hash() [2/5]

hash Qore::hash ( hash  h)

Returns the same hash with all complex types stripped.

Code Flags:
CONSTANT
Example:
hash h2 = hash(h1);
hash< auto > hash(object obj)
Returns a hash of an object's members.
Parameters
hthe hash to return
Returns
the same hash passed as an argument with all complex types stripped
Since
  • Qore 0.8.13 this function was modified to remove type information at runtime from typed hashes

◆ hash() [3/5]

hash<auto> Qore::hash ( list< auto >  l)

Returns a hash by taking even numbered list elements (starting with 0) and converting them to strings for the hash keys, and the odd numbered elements following the keys as the key value.

Code Flags:
RET_VALUE_ONLY
Parameters
lthe list to process in a manner similar to perl's hash initialization
Returns
a hash by taking even numbered list elements (starting with 0) and converting them to strings for the hash keys, and the odd numbered elements following the keys as the key value
Example:
hash<auto> h = hash(("a", 1, "b", "two"));

◆ hash() [4/5]

hash<auto> Qore::hash ( list< softstring >  keys,
list< auto >  values 
)

Returns a hash by taking the first list as a list of keys, and the second list as a list of values.

If the two lists are of unequal sizes, then the keys list takes precedence (if the values list is longer, excess values are ignored, if the keys list is longer, then excess elements are assigned NOTHING)

Code Flags:
RET_VALUE_ONLY
Parameters
keysa list of key values for the hash
valuesa list of values for the hash, corresponding to the elements in keys
Returns
a hash by taking the first list as a list of keys, and the second list as a list of values
Example:
hash<auto> h = hash(keysv, values);

◆ hash() [5/5]

hash<auto> Qore::hash ( object  obj)

Returns a hash of an object's members.

Note
that if this function is called from outside the class' scope, the hash will only contain the object's public members
Code Flags:
RET_VALUE_ONLY
Parameters
objthe object to process
Returns
a hash of an object's members
Example:
hash<auto> h = hash(obj);

◆ int() [1/3]

int Qore::int ( )

Always returns 0.

Code Flags:
CONSTANT

The int() function is used for type conversions, therefore this variant is not tagged with NOOP

See also
int(softint)

◆ int() [2/3]

int Qore::int ( softint  i)

Converts the argument to an integer value.

Code Flags:
CONSTANT
Parameters
ithe argument to convert to an integer
Returns
the integer value corresponding to the argument
Example:
int i = int("1");
int int(string str, int base=10)
Converts the string to an integer value with respect to the base.

◆ int() [3/3]

int Qore::int ( string  str,
int  base = 10 
)

Converts the string to an integer value with respect to the base.

If a character is out of range of the corresponding base only the substring preceeding the character is taken into account.
Base 0 means to interpret as a code literal, so that the actual base is 8, 10, or 16.
(Does the same as <string>::toInt(int base)).

Code Flags:
RET_VALUE_ONLY
Parameters
strthe string to convert to an integer
basethe base of the integer in the string; this value must be 0 or 2 - 36 inclusive or an INVALID-BASE exception will be thrown
Returns
the integer value corresponding to the arguments
Example:
int("fe", 16); // returns 254
int("34", 5); // returns 19
int("2p", 25); // returns 2 (p means 25, which is out of range)
int("2p", 26); // returns 77
int("2z", 36); // returns 107
int("21"); // returns 21 (base = 10)
int("21", 0); // returns 21 (base = 10)
int("021", 0); // returns 17 (base = 8)
int("0x21", 0); // returns 33 (base = 16)
Exceptions
INVALID-BASEthe base is invalid; must be 0 or 2 - 36 inclusive
See also
<string>::toInt(int base)
Since
Qore 0.8.5, this variant with the base argument is supported

◆ list()

list<auto> Qore::list (   ...)

Returns a list of the arguments passed at the top level.

Code Flags:
CONSTANT
Parameters
...the arguments that will be returned as a list
Returns
a list of the arguments passed at the top level; if a sole argument of NOTHING is passed, then an empty list is returned
Example:
list<auto> l = list(1, 2, 3, 4);
list< auto > list(...)
Returns a list of the arguments passed at the top level.

◆ number() [1/2]

number Qore::number ( )

Always returns 0.0.

Code Flags:
CONSTANT

The number() function is used for type conversions, therefore this variant is not tagged with NOOP

See also
number(softnumber)

◆ number() [2/2]

number Qore::number ( softnumber  n)

Converts the argument to a number value.

Code Flags:
CONSTANT
Parameters
nthe argument to convert to a number
Returns
the number value corresponding to the argument
Example:
number n = number("2.23040945718005e35");
number number(softnumber n)
Converts the argument to a number value.

◆ string() [1/2]

string Qore::string ( )

Always returns an empty string.

Code Flags:
CONSTANT

The string() function is used for type conversions, therefore this variant is not tagged with NOOP

See also
string(softstring)

◆ string() [2/2]

string Qore::string ( softstring  str,
*string  enc 
)

Converts the argument to a string.

Code Flags:
CONSTANT
Parameters
strthe argument to convert to a string
encthe encoding of the string returned
Returns
the string value corresponding to the argument(s)
Example:
string str = string(100);
string string(softstring str, *string enc)
Converts the argument to a string.
Since
the enc argument was added in Qore 0.8.12

◆ type()

string Qore::type ( auto  arg)

Returns a string giving the data type of the argument passed; see String Type Constants for the values returned by this function.

Code Flags:
CONSTANT
Parameters
argthe argument to check
Returns
a string giving the data type of the argument passed; see String Type Constants for the values returned by this function
Example:
string type = type(v);
string type(auto arg)
Returns a string giving the data type of the argument passed; see String Type Constants for the value...
Note
  • This function is identical to typename(any) and to calling pseudo-method <value>::type()
  • It is faster and more efficient to use <value>::typeCode() for comparing data types
See also
pseudo-method <value>::typeCode()

◆ typename()

string Qore::typename ( auto  arg)

Returns a string giving the data type of the argument passed; see String Type Constants for the values returned by this function.

Code Flags:
CONSTANT
Parameters
argthe argument to check
Returns
a string giving the data type of the argument passed; see String Type Constants for the values returned by this function
Example:
string type = typename(v);
string typename(auto arg)
Returns a string giving the data type of the argument passed; see String Type Constants for the value...
Note
  • This function is identical to type(any) and to calling pseudo-method <value>::type()
  • It is faster and more efficient to use <value>::typeCode() for comparing data types