English 中文(简体)
2. 已知的二恶英优化
原标题:Erlang binary optimization known integer

可进一步优化:

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"]
最佳回答

您可以发挥以下职能:

integer_value(Bin) ->
    integer_value(Bin, 0).

integer_value(<<Char, Tail/binary>>, Acc) when (Char >= $0) and (Char =< $9) ->
    integer_value(Tail, Acc * 10 + (Char - $0));
integer_value(<<1, Tail/binary>>, Acc) ->
    [Acc, Tail];
integer_value(Bin, _Acc) ->
    %% Throw an exception if the argument is not in the correct format
    erlang:error(badarg, [Bin]).

请打电话integer_ Value(<<”2345”,1,“restofmessageexistshere”>>

这一职能解决了你的问题,但正如前面的海报所说的那样,你可能想解释你想要做些什么,以确保这是解决你问题的最佳办法。

问题回答

暂无回答




相关问题
Printing several binary data fields from Google DataStore?

I m using Google App Engine and python for a web service. Some of the models (tables) I have in my web service have several binary data fields in them, and I d like to present this data to a computer ...

Handling large ActiveRecord object set as binary data

I have an issue with manipulating large number of record objects of type ActiveRecord model(Records are extracted from a complex operation but from the same table) and I want to pass that object set ...

How to send a push notification using Erlang?

I m trying to send a push notification to APNs using Erlang. This is the code I came up with so far: -module(apnstest2). -export([connect/0]). connect() -> application:start(ssl), ssl:...

What is a suitable buffer for Python s struct module

In Python I m accessing a binary file by reading it into a string and then using struct.unpack(...). Now I want to write to that string using struct.pack_into(...), but I get the error "Cannot use ...

Storing hexadecimal values as binary in MySQL

I was thinking about how I m storing passwords in my database : appropriately salted SHA1 strings in a CHAR(40) field. However, since the character data in there is actually just a hex representation ...

View ram in DOS

Is there a way in dos (im using a dos boot disk on a linux machine) to view portions of ram? ie. some form of command to read the binary at a given address? edit: my bootable floppy doesnt have ...

热门标签