[erlang-questions] checksum calculation

Gamoto <>
Mon Apr 6 11:34:51 CEST 2009


If it is not a very good solution, would you like to suggest a better one, for me and the other readers ?
>
>On 6 Apr 2009, at 11:29 am, Gamoto wrote:
>
>> I have a string of bytes and would like to compute the checksum  
>> which is the xor of all bytes.
>>
>> Let S the string, N the current byte
>>
>> checksum( <<N,Rest>>,Csum) ->
>>  checksum(Rest,Csum bxor N);
>>
>> The problems are: I don't know how to start (checksum(<<S>>) ???)  
>> and how to finish !
>
>checksum(Binary) ->
>     checksum(Binary, 0).
>
>checksum(<<>>, Sum) ->
>     Sum;
>checksum(<<Byte,Rest/binary>>, Sum) ->
>     checksum(Rest, Sum bxor Byte).
>
>>
>> First of all, is it the correct approach ?
>> (I suppose I rather should make Csum Exclusive-OR N modulo 8     ???)
>
>Why?
>The exclusive-OR of any number of bytes is still just one byte.
>
>Of course this isn't a very _good_ checksum,
>but that's another issue.
>




More information about the erlang-questions mailing list