[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Byte swapping and bindat
From: |
Stefan Monnier |
Subject: |
Re: Byte swapping and bindat |
Date: |
Fri, 10 Jun 2022 13:03:46 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) |
Stefan Monnier via Users list for the GNU Emacs text editor [2022-06-10
09:32:31] wrote:
>> Is there a way to specify a bindat type with fields whose endianess
>> depends on an external parameter (or another field of the type?)
>
> I'm so glad you asked:
>
> (let* ((threshold 32)
> (type (bindat-type (kind uint 8)
> (length sint 32 (> kind threshold)))))
> (list (bindat-unpack type "\x10\x00\x00\x01\x01")
> (bindat-unpack type "\x80\x00\x00\x01\x01")))
> =>
> (((kind . 16) (length . 257))
> ((kind . 128) (length . 16842752)))
>
> That's one of the benefits of the new Bindat :-)
Oh, and if you want to do that for `uint` (which currently doesn't take
a "reverse" argument (patch welcome)), you can do:
(let* ((threshold 32)
(type (bindat-type (kind uint 8)
(length if (> kind threshold)
(uintr 32) (uint 32)))))
(list (bindat-unpack type "\x10\x00\x00\x01\x01")
(bindat-unpack type "\x80\x00\x00\x01\x01")))
=>
(((kind . 16) (length . 257))
((kind . 128) (length . 16842752)))
-- Stefan