[erlang-questions] binary bad match error(how to get a dynamic width string from a binary body?)

Andreas Hillqvist andreas.hillqvist@REDACTED
Tue Mar 25 12:25:00 CET 2008


If i remeber correct, it would be possible to write it:
   <<UL:16, UserName:UL/binary, PL:16,
Password:PL/binary,Rest/binary>> =
<<0,3,97,98,99,0,6,49,50,51,52,53,54>>.
Or:
   read_string(<<UL:16, UserName:UL/binary, PL:16, Password:PL/binary,
Rest/binary>>) ->

But I do not have the possability to test to verify.


Kind regards
Andreas Hillqvist

2008/3/24, wenew zhang <wenewboy@REDACTED>:
> my protocol example:
> <<0,3,97,98,99,0,6,49,50,51,52,53,54>>
> 0,3 %% the width of the user name 97,98,99 is "abc"
> 0,6 %% the width of password "123456"
>
> code:
> read_string(Bin) ->
>    io:format("readstring~n"),
>    <<Len:16/integer, Bin1/binary>> = Bin,
>    io:format("Len:~w~n ",[Len]),
>    <<Str:Len,Rest/binary>> = Bin1,%% i want get the user name by
> Len,but it's fail
>    io:format("Str:len match successful!"),
>    io:format("Str:~w~n",[binary_to_list(Bin1)]),
>    {binary_to_list(Str),Rest}.
> i got error message:
> 2> readstring
> 2> Len:3
>  2>
> =ERROR REPORT==== 24-Mar-2008::13:46:51 ===
> ** State machine <0.40.0> terminating
> ** Last message in was {tcp,#Port<0.103>,
>                            <<0,3,97,98,99,0,6,49,50,51,52,53,54>>}
> ** When State == 'WAIT_FOR_DATA'
> **      Data  == {state,#Port<0.103>,{10,0,5,118}}
> ** Reason for termination =
> ** {{badmatch,<<97,98,99,0,6,49,50,51,52,53,54>>},
>    [{common,read_string,1},
>     {tcp_echo_fsm,'WAIT_FOR_DATA',2},
>     {gen_fsm,handle_msg,7},
>     {proc_lib,init_p,5}]}
> 2>
>
> best regards
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list