>,"81", <<1>>, <<"52=">>, [[50,48...">

Erlang - checksum

God's morning

I am trying to perform a checksum on the following function

Data = [<<"9">>,"81",
      <<1>>,
      <<"52=">>,
      [[50,48,49,48,49,48,50,54,45,49,53,":",52,53,":",52,52]],
      <<1>>,
      <<1>>,
      [<<"9">>,<<"0">>,<<1>>],
      [<<"5">>,<<"4">>,<<1>>]]

Using:

checksum(Data) ->  checksum(Data, 0).
checksum([H | T], Acc) ->
    if
        is_binary(H) ->
            I = binary_to_list(H); 
        true ->
            I = H 
    end,
    checksum(T,  I + Acc);

checksum([],  Acc) -> Acc.

Basically, you need to break the data into discrete numbers

ideally, it would look like [56,45,34,111,233, ...]

and then add them all together.

The compiler gives me errors no matter what I try. I decided this before it was very simple, but now one change in the food chain has affected it.

Please help, and best wishes!

+3
source share
4 answers

Try using the following code:

checksum(Data) ->                 checksum(iolist_to_binary(Data), 0).
checksum(<<I, T/binary>>, Acc) -> checksum(T,  I + Acc);
checksum(<<>>, Acc) ->            Acc.
+3
source

CRC, CRC32 Adler-32 erlang: crc32 erlang: adler32 BIF:

1> Data = [<<"9">>,"81",
1>       <<1>>,
1>       <<"52=">>,
1>       [[50,48,49,48,49,48,50,54,45,49,53,":",52,53,":",52,52]],
1>       <<1>>,
1>       <<1>>,
1>       [<<"9">>,<<"0">>,<<1>>],
1>       [<<"5">>,<<"4">>,<<1>>]]
1> .
[<<"9">>,"81",
 <<1>>,
 <<"52=">>,
 [[50,48,49,48,49,48,50,54,45,49,53,":",52,53,":",52,52]],
 <<1>>,
 <<1>>,
 [<<"9">>,<<"0">>,<<1>>],
 [<<"5">>,<<"4">>,<<1>>]]
2> erlang:adler32(Data).
1636173186
3> erlang:crc32(Data).
3649492735

erlang: phash2 BIF:

4> erlang:phash2(Data).     
38926910
5> erlang:phash2(Data, 65536).
64062
+2
if
    is_binary(H) ->
        I = binary_to_list(H); 
    true ->
        I = H

I H, binary_to_llist(H), .

checksum(T,  I + Acc);

I Acc, I . .

H, H - binary_to_list(H), H .

+1
checksum([A|B]) -> checksum(A) + checksum(B);
checksum([]) -> 0;
checksum(<<A, B/binary>>) -> A + checksum(B);
checksum(<<>>) -> 0;
checksum(A) -> A.
0

Source: https://habr.com/ru/post/1771532/


All Articles