【发布时间】:2011-02-10 12:57:18
【问题描述】:
在今天的工作中,我们拼凑了这个尝试:
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"
, "")
你有更好的方法吗?
以奥利弗的回答,我做了反向功能。
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)
)
};
【问题讨论】: