// this method has been added by Sebastien ROBERT // this implementation makes the mathematician-friendly approach programmer-friendly public byte[] EncodeEx(byte[] toEncode, int ecBytes) { if (ecBytes == 0) { throw new ArgumentException("No error correction bytes"); } int dataBytes = toEncode.Length - ecBytes; if (dataBytes <= 0) { throw new ArgumentException("No data bytes provided"); } GenericGFPoly generator = BuildGenerator(ecBytes); int[] infoCoefficients = toEncode.Select(x => (int)x).ToArray(); var info = new GenericGFPoly(field, infoCoefficients); info = info.MultiplyByMonomial(ecBytes, 1); GenericGFPoly remainder = info.Divide(generator)[1]; int[] coefficients = remainder.Coefficients; int numZeroCoefficients = ecBytes - coefficients.Length; return(Enumerable.Repeat <byte>(0, numZeroCoefficients) .Concat(coefficients.Select(x => (byte)x)) .ToArray()); }
public void Encode(int[] toEncode, int ecBytes) { if (ecBytes == 0) { throw new ArgumentException("No error correction bytes"); } int dataBytes = toEncode.Length - ecBytes; if (dataBytes <= 0) { throw new ArgumentException("No data bytes provided"); } GenericGFPoly generator = BuildGenerator(ecBytes); var infoCoefficients = new int[dataBytes]; Array.Copy(toEncode, 0, infoCoefficients, 0, dataBytes); var info = new GenericGFPoly(field, infoCoefficients); info = info.MultiplyByMonomial(ecBytes, 1); GenericGFPoly remainder = info.Divide(generator)[1]; int[] coefficients = remainder.Coefficients; int numZeroCoefficients = ecBytes - coefficients.Length; for (var i = 0; i < numZeroCoefficients; i++) { toEncode[dataBytes + i] = 0; } Array.Copy(coefficients, 0, toEncode, dataBytes + numZeroCoefficients, coefficients.Length); }