Search code examples
c#crccrc16

Convert C# unit test to a method


I have been given some code written as a unit test for xunit. I need to use this code in my application to generate a CRC-16. How can I get the code below to return what I need?

    [Fact]
    public void ComputesCrc16FromTelegramOf8000()
    {
        var calculator = new CRC16();

        var test = StringToByteArray("8000");
        var result = calculator.ComputeHash(test, 0, test.Length);
        Assert.Equal((ushort) 0xC061, BitConverter.ToUInt16(result, 0));
    }

Solution

  • You can just add some input and return the result:

    public Byte[] ComputeCrc16(string input) 
    {
        var calculator = new CRC16();
        var bytes = StringToByteArray(input);
    
        return calculator.ComputeHash(bytes, 0, bytes.Length);
    }