At work today, we threw together this attempt:
xquery version "1.0";
declare option saxon:output "omit-xml-declaration=yes";
declare variable $x := 99;
string-join(
for $b in (128,64,32,16,8,4,2,1)
let $xm := $x mod ($b*2)
return
if ( $xm >= $b ) then "1" else "0"
, "")
Do you have a better way?
Taking Oliver's answer, I have made the reverse function.
declare function local:bin-byte($x as xs:string) as xs:unsignedByte
{
let $binary-nibbles := ("0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111")
return xs:unsignedByte(
(index-of( $binary-nibbles, substring($x,1,4) )-1) * 16
+ (index-of( $binary-nibbles, substring($x,5,4) )-1)
)
};