Qore Programming Language Reference Manual  0.9.1
Type Conversion Functions

Functions

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 Qore::hash (object obj)
 Returns a hash of an object's members. More...
 
hash Qore::hash (list 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 Qore::hash (list keys, list 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 a hash without any key type information. More...
 
hash 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 Qore::list (...)
 Returns an untyped 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

◆ 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 ( 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() [3/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() [4/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_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");

◆ 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
Note
this function behaves differently when %strict-bool-eval is set

◆ float() [1/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() [2/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)

◆ hash() [1/5]

hash 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 h = hash(obj);

◆ hash() [2/5]

hash Qore::hash ( list  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 h = hash(("a", 1, "b", "two"));

◆ hash() [3/5]

hash Qore::hash ( list  keys,
list  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 h = hash(keysv, values);

◆ hash() [4/5]

hash Qore::hash ( hash  h)

Returns a hash without any key type information.

This function is used as an initializer and type converter

Code Flags:
CONSTANT
Parameters
hthe hash to return
Returns
an untyped hash identical to the argument but with any key type information removed
Example:
hash<string, int> h1(("string": 1));
# get an untyped hash value from h1
hash h2 = hash(h1);
Since
Qore 0.8.13 this function was modified to remove type information at runtime from typed hashes

◆ hash() [5/5]

hash 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

◆ int() [1/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

◆ 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() [3/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)

◆ list()

list Qore::list (   ...)

Returns an untyped list of the arguments passed at the top level.

Code Flags:
CONSTANT
Parameters
...the arguments that will be returned as a list
Returns
an untyped 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 l = list(1, 2, 3, 4);
Note
if the only argument to this function is a typed list, the type information will be removed in the return value

◆ number() [1/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() [2/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)

◆ string() [1/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);
Since
the enc argument was added in Qore 0.8.12

◆ string() [2/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)

◆ 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);
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);
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