7

I need to calculate CRC 16 of a string in elixir. Is there an existing library for the purpose?

Kshitij Mittal
  • 2,698
  • 3
  • 25
  • 40
  • 1
    There are some Erlang implementations: [https://github.com/search?utf8=✓&q=crc16++language%3AErlang&type=Code&ref=searchresults](https://github.com/search?utf8=✓&q=crc16++language%3AErlang&type=Code&ref=searchresults) – Lol4t0 Nov 04 '15 at 15:12

1 Answers1

3

I also needed a CRC library in elixir, I just published a package for this:

https://hex.pm/packages/crc https://github.com/TattdCodeMonkey/crc

TattdCodeMonkey
  • 155
  • 1
  • 7
  • Actually I need a library for CRC-16 (plain, not CCITT). http://www.lammertbies.nl/comm/info/crc-calculation.html , on this link a hex string - "test" (without quotes) gives 0xC481 as answer for CRC-16, which is correct. Can you suggest any library for plain CRC-16 ? – Kshitij Mittal Feb 26 '16 at 10:37
  • I already tried yours, but CCITT is not what I require right not. – Kshitij Mittal Feb 26 '16 at 10:38
  • Not off hand. If you can find a C library that does "plain" I could work on adding a function for it. In looking for libraries as examples I had a hard time finding "crc-16" everything was a specific flavor (ccitt, modbus, x_modem etc) – TattdCodeMonkey Feb 26 '16 at 20:47
  • Hi, I was able to find a CRC-16 implementation in C (exactly what I want) on this StackOverflow Question - http://stackoverflow.com/questions/10564491/function-to-calculate-a-crc16-checksum . The accepted answer seems to be exactly what I want (judging from the result it is giving). If you could implement that in your library, that would be great! – Kshitij Mittal Feb 29 '16 at 12:09
  • I will look into that this weekend, and comment back here. – TattdCodeMonkey Mar 04 '16 at 21:23
  • 1
    Updated https://hex.pm/packages/crc with a crc16 implementation based on the provided code. Test is passing according to crc given by crc online calculator. – TattdCodeMonkey Mar 05 '16 at 02:38
  • Thanks a lot! Bdw, will it work for hex inputs as well? – Kshitij Mittal Mar 06 '16 at 06:38
  • 1
    It takes a binary. but you can easily turn a hex number into a binary `number = 0x1234` `data = <>` – TattdCodeMonkey Mar 06 '16 at 15:39