Hi Guiseppe, 

1) You are right, it’s not too obvious what’s happening here: 
In BaseX the default serialization mode is „basex“ which tries to return items 
in a more readable way, hence internally your string is represented as 
xs:base64Binary — but when it is output to the query results panel it will be 
serialized according to whichever serialization is active; we opted for our 
custom serialization as we considered it to be a sane default, as it is able to 
serialize() all kinds of items (i.e. XML serialization won’t serialize maps or 
arrays) and is generally a little more readable than adaptive because we omit 
type information such as xs:base64Binary("c8Ogw6A=")

The following query serializes your value with different serialization 
parameters. 
> let $it := convert:string-to-base64("sàà")
> 
> for $method in (
>   "basex", "xml", "adaptive", "json", "text","html"
> )
> return element { $method } {  
>   serialize( 
>     $it,
>     map{ "method": $method }
>   )
> }



2) The length method basically tells you how many bytes your string needs to be 
encoded as utf8, you may use the following query and try it yourself:
> let $str := "•"
> let $bin := convert:string-to-base64($str)
> return element _ {
>   element str { $str },
>   element b64 { $bin },
>   element octets {
>    attribute length { bin:length($bin) },
>    for $byte in $bin => convert:binary-to-integers() 
>    return element octet {
>      $byte => convert:integer-to-base(2)
>    }
>   }  
> }

Depending on the input string and it’s encoding (i.e. ‚a' will only need one 
byte, but ‚ä' already needs two, ‚•' will even need three in utf8) your string 
is converted to xs:base64Binary and the bin:length() function will count how 
many octets are needed for this representation.

Hope this helps :-)

Best
Michael


> Am 14.08.2019 um 02:22 schrieb Giuseppe G. A. Celano 
> <cel...@informatik.uni-leipzig.de>:
> 
> Hi
> 
> I am playing around with the binary module. I have two simple questions:
> 
> 1) convert:string-to-base64("sàà") returns sàà : what does it mean? I see in 
> the documentation that I need to use string() to see the value of 
> xs:base64Binary (c8Ogw6A=), but shouldn't xs:base64Binary already be 
> outputted as c8Ogw6A= ? (This is actually displayed in the Info Panel).
> 
> 2) bin:length(convert:integers-to-base64(x)) always returns one number, no 
> matter how big the number is. In the documentation I read that the output of 
> bin:length should be the size of binary data in octects: how is that possible?
> 
> Thanks,
> Giuseppe
> 
> 

Reply via email to