public override (BitList, VersionInfo) Encode(string content, ErrorCorrectionLevel errorCorrectionLevel) { if (content == null) { throw new ArgumentNullException(nameof(content)); } byte[] data = TextEncoding.UTF8.GetBytes(content); EncodingMode encodingMode = EncodingMode.Byte; var versionInfo = VersionInfo.FindSmallestVersionInfo(errorCorrectionLevel, encodingMode, data.Length * 8); if (versionInfo == null) { throw new InvalidOperationException("Too much data to encode"); } var bits = new BitList(); bits.AddBits((uint)encodingMode, 4); bits.AddBits((uint)content.Length, versionInfo.CharCountBits(encodingMode)); foreach (var b in data) { bits.AddByte(b); } AddPaddingAndTerminator(ref bits, versionInfo); return(bits, versionInfo); }
public void FindSmallestVersionInfo_NumberOfDataBitsOutOfRange_ShouldReturnNull() { // Act var versionInfo = VersionInfo.FindSmallestVersionInfo(ErrorCorrectionLevel.H, EncodingMode.AlphaNumeric, 10208); // Assert versionInfo.Should().BeNull(); }
public void FindSmallestVersionInfo_ShouldReturnCorrectVersionForGivenNumberOfDataBits(int dataBits, byte expectedVersion) { // Act var versionInfo = VersionInfo.FindSmallestVersionInfo(ErrorCorrectionLevel.H, EncodingMode.AlphaNumeric, dataBits); // Assert versionInfo.Should().NotBeNull(); versionInfo.Version.Should().Be(expectedVersion); }
public override (BitList, VersionInfo) Encode(string content, ErrorCorrectionLevel errorCorrectionLevel) { if (content == null) { throw new ArgumentNullException(nameof(content)); } bool contentLengthIsOdd = content.Length % 2 == 1; int contentBitCount = (content.Length / 2) * 11; if (contentLengthIsOdd) { contentBitCount += 6; } EncodingMode encodingMode = EncodingMode.AlphaNumeric; var versionInfo = VersionInfo.FindSmallestVersionInfo(errorCorrectionLevel, encodingMode, contentBitCount); if (versionInfo == null) { throw new InvalidOperationException("Too much data to encode"); } var bits = new BitList(); bits.AddBits((uint)encodingMode, 4); bits.AddBits((uint)content.Length, versionInfo.CharCountBits(encodingMode)); var encoder = new Queue <int>(content.Select(x => CharSet.IndexOf(x))); for (int i = 0; i < content.Length / 2; i++) { int c1 = encoder.Dequeue(); int c2 = encoder.Dequeue(); if (c1 < 0 || c2 < 0) { throw new InvalidOperationException($"{content} can not be ancoded as {encodingMode}"); } bits.AddBits((uint)(c1 * 45 + c2), 11); } if (contentLengthIsOdd) { int c = encoder.Dequeue(); if (c < 0) { throw new InvalidOperationException($"{content} can not be ancoded as {encodingMode}"); } bits.AddBits((uint)c, 6); } AddPaddingAndTerminator(ref bits, versionInfo); return(bits, versionInfo); }
public override (BitList, VersionInfo) Encode(string content, ErrorCorrectionLevel errorCorrectionLevel) { if (content == null) { throw new ArgumentNullException(nameof(content)); } var contentBitCount = (content.Length / 3) * 10; switch (content.Length % 3) { case 1: contentBitCount += 4; break; case 2: contentBitCount += 7; break; } EncodingMode encodingMode = EncodingMode.Numeric; var versionInfo = VersionInfo.FindSmallestVersionInfo(errorCorrectionLevel, encodingMode, contentBitCount); if (versionInfo == null) { throw new InvalidOperationException("Too much data to encode"); } var bits = new BitList(); bits.AddBits((uint)encodingMode, 4); bits.AddBits((uint)content.Length, versionInfo.CharCountBits(encodingMode)); for (int i = 0; i < content.Length; i += 3) { string currentContentPart = i + 3 <= content.Length ? content.Substring(i, 3) : content.Substring(i); if (!uint.TryParse(currentContentPart, out uint currentNumericalValue)) { throw new InvalidOperationException($"{content} can not be ancoded as {encodingMode}"); } byte bitCount; switch (currentContentPart.Length % 3) { case 0: bitCount = 10; break; case 1: bitCount = 4; break; case 2: bitCount = 7; break; default: throw new InvalidOperationException(); } bits.AddBits(currentNumericalValue, bitCount); } AddPaddingAndTerminator(ref bits, versionInfo); return(bits, versionInfo); }