Can this be further optimized:
Binary = <<"2345", 1, "restofmessageexistshere">>
get_integer_value(Binary) ->
[Num, _, LastRest] = integer_value(Binary),
[Num, LastRest].
integer_value(<<1, _Rest/binary>>) -> [0, 1, _Rest];
integer_value(<<H:8, Rest/binary>>) ->
% io:format("~n~p~n", [Rest]),
[Num, Exp, LastRest] = integer_value(Rest),
[(H-48)*Exp + Num, Exp*10, LastRest].
Expected Result -> [2345, "restofmessageexistshere"]
You could use a function like the following one:
If you call
integer_value(<<"2345", 1, "restofmessageexistshere">>)you’ll get[2345, "restofmessageexistshere"].This function solves your problem, but as the previous poster said, you might want to explain what you want to do to make sure that this is the best solution for your problem.