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).

Prototype

binary binary(string) (CONST)

binary binary(binary) (CONST)

binary binary(null) (CONST)

binary binary() (CONST)

Example
$bin = binary("hello");

Table 3.86. Arguments and Return Values for binary()

Argument Type

Return Type

Description

softstring

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().

Prototype

string binary_to_string(binary $bin)

string binary_to_string(binary $bin, string $encoding)

Example
$str = binary_to_string($bin, "utf8");

Table 3.87. Arguments and Return Values for binary_to_string()

Argument Type

Return Type

Description

binary $bin, [string $encoding]

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.


3.4.3. boolean()

Synopsis

Converts the argument to a boolean value.

Prototype

bool boolean(softbool) (CONST)

bool boolean(null) (CONST)

bool boolean() (CONST)

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

Table 3.88. Arguments and Return Values for boolean()

Argument Type

Return Type

Description

softbool

bool

Converts the argument to an boolean, 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. When converting from an integer, the integer value represents the offset in seconds from 1970-01-01 in the local time zone.

See also date_ms(), date_us(), and localtime().

As of Qore 0.8.0, converting a date from a string is much more flexible; formats such as the following are now accepted (note that in the following examples, if the time zone/UTC offset is not explicitly specified, the local time zone is assumed):

Table 3.89. Examples Converting Date From String

String

Date

"2001-01-01T15:35:23"

2001-01-01 15:35:23 Mon +01:00 (CET)

"20010101 15:35:23Z"

2001-01-01 15:35:23 Mon Z (UTC)

"20010101 153523Z"

2001-01-01 15:35:23 Mon Z (UTC)

"20010101 153523-02"

2001-01-01 15:35:23 Mon -02:00 (-02:00)

"20010101 153523-02:00:00"

2001-01-01 15:35:23 Mon -02:00 (-02:00)

"2001-01-01-153523-020000"

2001-01-01 15:35:23 Mon -02:00 (-02:00)

"20010101-153523"

2001-01-01 15:35:23 Mon +01:00 (CET)


Prototype

date date(string) (CONST)

date date(string, string) (CONST)

date date(softint) (CONST)

date date(null) (CONST)

date date() (CONST)

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

Table 3.90. Arguments and Return Values for date()

Argument Type

Return Type

Description

string

date

Converts the argument to a date and returns the date.

string, string

date

Converts the 1st argument to a date by the given conversion mask (2nd arg) and returns the date. Example: date("20100401 234520", "YYYYMMDD HHmmSS")

softint

date

The argument is assumed to be the number of seconds since 1970-01-01 in the local time zone; this value is used to produce the date value that is returned.


Table 3.91. String to Date conversion masks

Symbol

Description

YY

Year's last two digits. The the current year's century is used for new date

YYYY

Year with the all digits (length 4) like 1956, 0870, or ' 546' with space as the placeholder

MM

Month number (length 2) like 11, 04, or ' 1' with space as the placeholder

Mon

Month name abbrevation (length 3) with the first letter uppercased. Example: Apr for April

MON

Month name abbrevation (length 3) with all letters uppercased. Example: APR for April

DD

Date number (length 2) like 29, 04, or ' 1' with space as the placeholder

mm

Minute number (length 2) like 11, 04, or ' 1' with space as the placeholder

SS

Second number (length 2) like 11, 04, or ' 1' with space as the placeholder

ss

Millisecond number (length 2) like 11, 04, or ' 1' with space as the placeholder


This function does not throw any exceptions - except for date(string, string). It can throw DATE-CONVERT-ERROR exception.

3.4.5. float()

Synopsis

Converts the argument passed to a floating-point value.

Prototype

float float(softfloat) (CONST)

float float(null) (CONST)

float float() (CONST)

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

Table 3.92. Arguments and Return Values for float()

Argument Type

Return Type

Description

softfloat

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.

Prototype

hash hash(list $list)

hash hash(list $keys, list $values)

hash hash(hash $hash) (CONST)

hash hash(object $obj)

hash hash() (CONST)

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

Table 3.93. Arguments and Return Values for hash()

Argument Type

Return Type

Description

list $keys, list $values

hash

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).

list $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.

hash $hash

hash

Returns the hash argument unmodified.

object $obj

hash

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

any

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.

Prototype

int int(softint) (CONST)

int int(null) (CONST)

int int() (CONST)

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

Table 3.94. Arguments and Return Values for int()

Argument Type

Return Type

Description

softint

int

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.

Prototype

list list(...) (CONST)

Example
$l = list(200);

Table 3.95. 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; if no argument is passed, and empty list is 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.

Prototype

string string(softstring) (CONST)

string string() (CONST)

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

Table 3.96. Arguments and Return Values for string()

Argument Type

Return Type

Description

softstring

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..

Prototype

string type(any) (CONST)

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

Table 3.97. 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.

Prototype
typename(expression)

Table 3.98. 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.