2,876 bytes added
, 02:38, 27 May 2012
This [[Module Library|XQuery Module]] contains functions to convert data between different formats.
=Conventions=
All functions in this module are assigned to the {{Code|http://basex.org/modules/convert}} namespace, which is statically bound to the {{Code|convert}} prefix.<br/>
All errors are assigned to the {{Code|http://basex.org/errors}} namespace, which is statically bound to the {{Code|bxerr}} prefix.
=Functions=
==convert:to-bytes==
{|
|-
| width='90' | '''Signatures'''
|{{Func|util:to-bytes|$bin as xs:binary|xs:byte*}}
|-
| '''Summary'''
|Extracts the bytes from the given binary data {{Code|$bin}}.
|-
| '''Examples'''
|
* <code>util:to-bytes(xs:base64Binary('QmFzZVggaXMgY29vbA=='))</code> returns the sequence {{Code|(66, 97, 115, 101, 88, 32, 105, 115, 32, 99, 111, 111, 108)}}.
* {{Code|util:to-bytes(xs:hexBinary("4261736558"))}} returns the sequence {{Code|(66 97 115 101 88)}}.
|}
==convert:to-string==
{|
|-
| width='90' | '''Signatures'''
|{{Func|util:to-string|$bytes as xs:binary, $encoding as xs:string|xs:string}}
|-
| '''Summary'''
|Converts the specifed {{Code|bytes}} to a string, using the optional {{Code|$encoding}}.
|-
| '''Examples'''
|
* {{Code|util:to-string(xs:hexBinary('48656c6c6f576f726c64'))}} returns the string {{Code|HelloWorld}}.
|}
=Numeric Functions=
==util:integer-to-base==
{|
|-
| width='90' | '''Signatures'''
|{{Func|util:integer-to-base|$num as xs:integer, $base as xs:integer|xs:string}}<br />
|-
| '''Summary'''
|Converts {{Code|$num}} to base {{Code|$base}}, interpreting it as a 64-bit unsigned integer.<br />The first {{Code|$base}} elements of the sequence {{Code|'0',..,'9','a',..,'z'}} are used as digits.<br />Valid bases are {{Code|2, .., 36}}.<br />
|-
| '''Examples'''
|
* {{Code|util:integer-to-base(-1, 16)}} returns the hexadecimal string {{Code|'ffffffffffffffff'}}.
* {{Code|util:integer-to-base(22, 5)}} returns {{Code|'42'}}.
|}
==util:integer-from-base==
{|
|-
| width='90' | '''Signatures'''
|{{Func|util:integer-from-base|$str as xs:string, $base as xs:integer|xs:integer}}<br />
|-
| '''Summary'''
|Decodes an {{Code|xs:integer}} from {{Code|$str}}, assuming that it's encoded in base {{Code|$base}}.<br /> The first {{Code|$base}} elements of the sequence {{Code|'0',..,'9','a',..,'z'}} are allowed as digits, case doesn't matter. <br />Valid bases are {{Code|2, .., 36}}.<br /> If {{Code|$str}} contains more than 64 bits of information, the result is truncated arbitarily.
|-
| '''Examples'''
|
* {{Code|util:integer-from-base('ffffffffffffffff', 16)}} returns {{Code|-1}}.
* {{Code|util:integer-from-base('CAFEBABE', 16)}} returns {{Code|3405691582}}.
* {{Code|util:integer-from-base('42', 5)}} returns {{Code|22}}.
* {{Code|util:integer-from-base(util:integer-to-base(123, 7), 7)}} returns {{Code|123}}.
|}
=Changelog=
The module was introduced with Version 7.3.
[[Category:XQuery]]