public void CalculateCrcForSingleCommand() { var data = new byte[] { 0x09 }; var crc = CheckSumCalculator.Calculate(data); Assert.Equal(0xD7, crc.High); Assert.Equal(0xEB, crc.Low); }
public void CalculateCrcForLongCommand() { var data = new byte[] { 0x1C, 0xD7, 0x09 }; var crc = CheckSumCalculator.Calculate(data); Assert.Equal(0x5E, crc.High); Assert.Equal(0x13, crc.Low); }
public void Return_The_CheckSum_Of_The_Puzzle_Entered() { List <string> rowsList = new List <string> { "5 1 9 5", "7 5 3", "2 4 6 8" }; CheckSumCalculator checkSumCalculator = new CheckSumCalculator(rowsList); Check.That(checkSumCalculator.Calculate()).IsEqualTo(18); }
public IValidationResult Validate(Entity entity) { var weights = new[] { 1, 3, 7, 9, 1, 3, 7, 9, 1, 3 }; var calculator = new CheckSumCalculator(); var checkSum = calculator.Calculate(weights, entity.Pesel.Substring(0, 10)); if (checkSum == (int)char.GetNumericValue(entity.Pesel[10])) { entity.PeselStruct.ControlNumber = Convert.ToByte(checkSum); return(new OkValidationResult()); } return(new InvalidCheckSumValidationResult()); }
public ArraySegment <byte> Read() { SkipBeginMarker(); var content = ReadContent().ToArray(); var crc = CheckSumCalculator.Calculate(content, 0, content.Length - 2); if (crc.High != content[content.Length - 2] || crc.Low != content[content.Length - 1]) { throw new InvalidDataException("CRC verification failed"); } var result = new ArraySegment <byte>(content, 0, content.Length - 2); return(result); }
private async Task Write(byte[] packet, CancellationToken token) { stream.WriteByte(frameSynchronizationMarker); stream.WriteByte(frameSynchronizationMarker); foreach (var b in packet) { stream.WriteByte(b); if (b == frameSynchronizationMarker) { stream.WriteByte(0xF0); } } var crc = CheckSumCalculator.Calculate(packet); stream.WriteByte(crc.High); stream.WriteByte(crc.Low); stream.WriteByte(frameSynchronizationMarker); stream.WriteByte(frameCompletedMarker); await stream.FlushAsync(token); }