[erlang-questions] Binary comprehensions again.

Anthony Ramine nox@REDACTED
Sun Mar 20 17:51:16 CET 2011


Le 18 mars 2011 à 22:33, Bob Cowdery a écrit :

> Hi
> This is another part of my code that works well and is pretty efficient
> (thanks to help from the list).
> 
> << <<(I/?S):32/float, (Q/?S):32/float>> || <<I:24/big-signed,
> Q:24/big-signed, _M:16/big-signed>> <= Bin >>
> 
> I now need to do the same kind of thing in reverse.
> This is not correct code, it won't even compile but it illustrates the
> logic I want.
> 
> << <<L:16/big-signed, R:16/big-signed, I:16, Q:16>> ||
> <<round(L*?M):32/float, round(R*?M):32/float, I:0, Q:0>> <= Bin >>.
> 
> There are several problems in here and I don't know if the solution can
> be quite this concise. The input binary is a sequence of two floats
> which are value +1 to -1. M is a scale factor (2 bsl 15) - 1 which gives
> a value that fits in a signed 16 bit integer. The output needs to be 4
> integers where the last two are just padding for the present. I don't
> know how to do that so I bound the variables to zero bits. It does not
> seem to like the function call in there but I need to do the equivalent
> of a cast somehow.
> 
> Any hints on how to form that correctly would be appreciated.
> 
> Bob

You can't have expressions which aren't constant in patterns so your code
obviously does not compile.

You want something along those lines:

<< <<(round(L*?M):24/big-signed, (round(R*?M)):24/big-signed, 0:16>> ||
   <<L:32/float, R:32/float>> <= Bin >>.

Regards,
--
Anthony Ramine
Dev:Extend
http://dev-extend.eu






More information about the erlang-questions mailing list