Skip to content

Commit 389082a

Browse files
committed
Add overloads to AlphanumericEncoder
1 parent a6719ff commit 389082a

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

QRCoder/QRCodeGenerator/AlphanumericEncoder.cs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,17 @@ private static Dictionary<char, int> CreateAlphanumEncDict(char[] alphanumEncTab
5252
/// <returns>The number of bits required to encode the text.</returns>
5353
public static int GetBitLength(string plainText)
5454
{
55-
return (plainText.Length / 2) * 11 + (plainText.Length & 1) * 6;
55+
return GetBitLength(plainText.Length);
56+
}
57+
58+
/// <summary>
59+
/// Calculates the bit length required to encode alphanumeric text of the given length.
60+
/// </summary>
61+
/// <param name="textLength">The length of the alphanumeric text to be encoded.</param>
62+
/// <returns>The number of bits required to encode the text.</returns>
63+
public static int GetBitLength(int textLength)
64+
{
65+
return (textLength / 2) * 11 + (textLength & 1) * 6;
5666
}
5767

5868
/// <summary>
@@ -64,8 +74,22 @@ public static int GetBitLength(string plainText)
6474
/// <returns>A BitArray representing the binary data of the encoded alphanumeric text.</returns>
6575
public static BitArray GetBitArray(string plainText)
6676
{
67-
var codeText = new BitArray(GetBitLength(plainText));
68-
WriteToBitArray(plainText, codeText, 0);
77+
return GetBitArray(plainText, 0, plainText.Length);
78+
}
79+
80+
/// <summary>
81+
/// Converts a substring of alphanumeric plain text into a binary format optimized for QR codes.
82+
/// Alphanumeric encoding packs characters into 11-bit groups for each pair of characters,
83+
/// and 6 bits for a single remaining character if the total count is odd.
84+
/// </summary>
85+
/// <param name="plainText">The string containing the alphanumeric text to be encoded.</param>
86+
/// <param name="index">The starting position within the string.</param>
87+
/// <param name="count">The number of characters to encode.</param>
88+
/// <returns>A BitArray representing the binary data of the encoded alphanumeric text.</returns>
89+
public static BitArray GetBitArray(string plainText, int index, int count)
90+
{
91+
var codeText = new BitArray(GetBitLength(count));
92+
WriteToBitArray(plainText, index, count, codeText, 0);
6993
return codeText;
7094
}
7195

@@ -80,9 +104,22 @@ public static BitArray GetBitArray(string plainText)
80104
/// <returns>The next index in the BitArray after the last bit written.</returns>
81105
public static int WriteToBitArray(string plainText, BitArray codeText, int codeIndex)
82106
{
83-
var index = 0;
84-
var count = plainText.Length;
107+
return WriteToBitArray(plainText, 0, plainText.Length, codeText, codeIndex);
108+
}
85109

110+
/// <summary>
111+
/// Writes a substring of alphanumeric plain text directly into an existing BitArray at the specified index.
112+
/// Alphanumeric encoding packs characters into 11-bit groups for each pair of characters,
113+
/// and 6 bits for a single remaining character if the total count is odd.
114+
/// </summary>
115+
/// <param name="plainText">The string containing the alphanumeric text to be encoded.</param>
116+
/// <param name="index">The starting position within the string.</param>
117+
/// <param name="count">The number of characters to encode.</param>
118+
/// <param name="codeText">The target BitArray to write to.</param>
119+
/// <param name="codeIndex">The starting index in the BitArray where writing should begin.</param>
120+
/// <returns>The next index in the BitArray after the last bit written.</returns>
121+
public static int WriteToBitArray(string plainText, int index, int count, BitArray codeText, int codeIndex)
122+
{
86123
// Process each pair of characters.
87124
while (count >= 2)
88125
{

0 commit comments

Comments
 (0)