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

wenew zhang wenewboy@REDACTED
Mon Mar 24 06:58:42 CET 2008


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



More information about the erlang-questions mailing list