[erlang-questions] segments of variable length

zambal <>
Sat Apr 4 15:26:40 CEST 2009


On 4 apr, 14:03, "Gamoto" <> wrote:
>
> start()->
>   case binary_to_term(?X) of
>            {<<H,U,T>>}->
>             io:format("Header = ~s~n",[H]),
>             io:format("Unit ID = ~s~n",[U]),
>             io:format("Time = ~s~n",[T])
>        end.
>
> This gives me an error.
> I was waiting for
> Header = $AVRMC
> Unit ID = 1234
> Time =  151736
>
> The most important for me is to obtain H,U and T

If the data you're trying to recieve is send like this:
 term_to_binary( {$AVRMC, 1234, 151736} )

then your case statement should look like this:

   case binary_to_term(?X) of
            {H,U,T}->
             io:format("Header = ~s~n",[H]),
             io:format("Unit ID = ~s~n",[U]),
             io:format("Time = ~s~n",[T])
    end.

(note the absence of the '<<' and '>>' operators in the match pattern)

---
vincent



More information about the erlang-questions mailing list