3.4. Qore Type Functions

3.4.1. binary()

Synopsis

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 some reason, 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).

Usage
binary(string)
Example
$bin = binary("hello");

Table 3.72. Arguments and Return Values for binary()

Argument Type

Return Type

Description

String

Binary

A binary data type holding the string data passed.


This function does not throw any exceptions.

3.4.2. binary_to_string()

Synopsis

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 encoding is assumed.

No checking is performed for embedded null characters or for character encoding violations; the data is simply copied from the binary value to the string (with any embedded nulls, if present), and the string is tagged with the given encoding or with the default encoding if no second argument is passed. See also string() and binary().

Usage
binary_to_string(binary, [encoding])
Example
$str = binary_to_string($bin, "utf8");

Table 3.73. Arguments and Return Values for binary_to_string()

Argument Type

Return Type

Description

Binary, [String]

String

Returns a string corresponding to the binary data passed, taking an optional second argument giving the string encoding. If no second argument is passed then the default encoding is assumed.


Table 3.74. Exceptions Thrown by binary_to_string()

err

desc

BINARY-TO-STRING-ERROR

No binary value was passed as the first argument.


3.4.3. boolean()

Synopsis

Converts the argument to a boolean value.

Usage
boolean(expression)
Example
$bool = boolean(1); # returns True

Table 3.75. Arguments and Return Values for boolean()

Argument Type

Return Type

Description

Any

Boolean

Converts the argument to an integer if necessary, where any non-zero value is True, zero is False.


This function does not throw any exceptions.

3.4.4. date()

Synopsis

Converts the argument to a date and returns the date.

Usage
date(expression)
Example
$date = date(1); # return 1970-01-01T00:00:01

Table 3.76. Arguments and Return Values for date()

Argument Type

Return Type

Description

Any

Date

Converts the argument to a date and returns the date.


This function does not throw any exceptions.

3.4.5. float()

Synopsis

Converts the argument passed to a floating-point value.

Usage
float(expression)
Example
$float = float("1.435"); # returns 1.435

Table 3.77. Arguments and Return Values for float()

Argument Type

Return Type

Description

Any

Float

Converts argument passed to a floating-point value.


This function does not throw any exceptions.

3.4.6. hash()

Synopsis

Converts an object or a list to a hash; for any other argument, returns an empty hash (ignores any other arguments passed).

For an object argument, the hash returned is equal to the object members (excluding private members if called outside the class); a list is converted to 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.

Usage
hash(list | object)
Example
$h = hash($object); # creates a hash of the object's members

Table 3.78. Arguments and Return Values for hash()

Argument Type

Return Type

Description

Object

Hash

Returns a hash of the object's members (public members only if called outside the class).

List

Hash

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.

anything other than Object or List

Hash

Returns an empty hash.


This function does not throw any exceptions.

3.4.7. int()

Synopsis

Converts the argument passed to an integer value if it is not already.

Usage
int(expression)
Example
$int = int("200"); # returns 200

Table 3.79. Arguments and Return Values for int()

Argument Type

Return Type

Description

Any

Integer

Converts argument passed to an integer value.


This function does not throw any exceptions.

3.4.8. list()

Synopsis

Returns a list; if any arguments are passed, they are inserted as the first element in the list returned.

Usage
list([expression])
Example
$l = list(200);

Table 3.80. Arguments and Return Values for list()

Argument Type

Return Type

Description

[Any ...]

List

Returns a list. If any arguments are passed, they are inserted as the first element in the list returned.


This function does not throw any exceptions.

3.4.9. string()

Synopsis

Converts the argument passed to a string value.

This function will not convert a binary value to a string; in order to do this, use the binary_to_string() function.

Usage
string(expression)
Example
$str = string(200); # returns "200"

Table 3.81. Arguments and Return Values for string()

Argument Type

Return Type

Description

Any

String

Converts the argument passed to a string value.


This function does not throw any exceptions.

3.4.10. type()

Synopsis

Returns the data type of the argument passed. See Type Constants for the values returned by this function..

Usage
type(expression)
Example
$type = type("hello"); # returns Type::String ("string")

Table 3.82. Arguments and Return Values for type()

Argument Type

Return Type

Description

Any

String

Returns the data type of the argument passed. See Type_Constants for the values that can be returned by this function.


This function does not throw any exceptions.

3.4.11. typename()

Synopsis

deprecated: use type() instead.

Usage
typename(expression)

Table 3.83. Arguments and Return Values for typename()

Argument Type

Return Type

Description

Any

String

deprecated: use type() instead.


This function does not throw any exceptions.

Note

This function will be removed in a future version of Qore.