Qore Programming Language Reference Manual: Type Conversion Functions

Qore Programming Language Reference Manual 0.8.11

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes

All Classes Namespaces Functions Variables Groups Pages
Functions
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 itself. More...
hash | Qore::hash ()
| Always returns the same hash passed. More...
int | Qore::int (string str, softint base)
| Converts the argument to an integer value. 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 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)
| Converts the argument to a string. More...
string | Qore::string ()
| Always returns an empty string. More...
string | Qore::type (any 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 (any 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 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(softstring)
  - binary(null)

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 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:
  my binary $b = binary($str);

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

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

  b | the binary object to convert directly to a string
  encoding | the 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:
  my string $str = binary_to_string($b, "iso-8859-1");

bool Qore::boolean | ( | any | arg | ) |

Converts the argument to a boolean value.

Code Flags:
  CONSTANT

Parameters

  arg | the argument to convert to a boolean

Returns
  the boolean value corresponding to the argument

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

Note
  this function behaves differently when %strict-bool-eval is set

float Qore::float | ( | softfloat | f | ) |

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

Code Flags:
  CONSTANT

Parameters

  f | the argument to convert to a float

Returns
  the float value corresponding to the argument

Example:
  my float $i = float("3.1415");

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

  obj | the object to process

Returns
  a hash of an object's members

Example:
  my hash $h = hash($obj);

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

  l | the 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:
  my hash $h = hash(("a", 1, "b", "two"));

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

  keys | a list of key values for the hash
  values | a 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:
  my hash $h = list($keys, $values);

hash Qore::hash | ( | hash | h | ) |

Returns itself.

This function is used as an initializer and type converter, so this identity-variant is provided for consistency's sake

Code Flags:
  CONSTANT

Parameters

  h | the has to return

Returns
  the hash passed

Example:
  my hash $h = hash($h);

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 Qore::int | ( | string | str,
|  | softint | base
| ) |  |

Converts the argument to an integer value.

Code Flags:
  RET_VALUE_ONLY

Parameters

  str | the string to convert to an integer
  base | the 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:
  my int $i = int("fabc9", 16);

Exceptions

  INVALID-BASE | the base is invalid; must be 0 or 2 - 36 inclusive

Since
  Qore 0.8.5, this variant with the base argument is supported

int Qore::int | ( | softint | i | ) |

Converts the argument to an integer value.

Code Flags:
  CONSTANT

Parameters

  i | the argument to convert to an integer

Returns
  the integer value corresponding to the argument

Example:
  my int $i = int("1");

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 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:
  my list $l = list(1, 2, 3, 4);

number Qore::number | ( | softnumber | n | ) |

Converts the argument to a number value.

Code Flags:
  CONSTANT

Parameters

  n | the argument to convert to a number

Returns
  the number value corresponding to the argument

Example:
  my number $n = number("2.23040945718005e35");

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 Qore::string | ( | softstring | str | ) |

Converts the argument to a string.

Code Flags:
  CONSTANT

Parameters

  str | the argument to convert to a string

Returns
  the string value corresponding to the argument

Example:
  my string $str = string(100);

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 Qore::type | ( | any | 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

  arg | the 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:
  my 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()

string Qore::typename | ( | any | 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

  arg | the 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:
  my 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