Skip to content

Instantly share code, notes, and snippets.

@CliffordAnderson
Last active December 30, 2016 02:43
Show Gist options
  • Save CliffordAnderson/77d8a2407e1c8bcbd48cceb304f486f4 to your computer and use it in GitHub Desktop.
Save CliffordAnderson/77d8a2407e1c8bcbd48cceb304f486f4 to your computer and use it in GitHub Desktop.
Convert from Base 10
xquery version "3.1";
(: Converts positive integers between base 10 and base 2-10 :)
declare function local:convert-base($num as xs:integer, $base as xs:integer) as xs:integer*
{
if ($num > 0 and ($base gt 1 and $base lt 11))
then local:join-nums((local:convert-base($num idiv $base, $base), $num mod $base))
else ()
};
declare function local:join-nums($num as xs:integer*) as xs:integer
{
xs:integer(fn:string-join($num))
};
local:convert-base(1024,2)
@CliffordAnderson
Copy link
Author

There are surely more elegant ways to accomplish this conversion. If you know a better method in XQuery, I'd be glad to compare it to mine. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment