Fix parsing of hexadecimal strings with odd number of characters

This commit is contained in:
Arnaud TAMAILLON 2024-09-01 08:21:18 +02:00 committed by BobLd
parent 05e6a894d0
commit 4f2a0976e3
2 changed files with 6 additions and 1 deletions

View File

@ -9,6 +9,8 @@
[InlineData("61", "a")]
[InlineData("0061", "a")]
[InlineData("7465787420736f", "text so")]
[InlineData("6170", "ap")]
[InlineData("617", "ap")]
public void MapsCorrectlyToString(string input, string expected)
{
var token = new HexToken(input.ToCharArray());

View File

@ -63,7 +63,10 @@ namespace UglyToad.PdfPig.Tokens
throw new ArgumentNullException(nameof(characters));
}
var bytes = new byte[characters.Length / 2];
// if the final character is missing, it is considered to be a 0, as per 7.3.4.3
// adding 1 to the characters array length ensure the size of the byte array is correct
// in all situations
var bytes = new byte[(characters.Length+1) / 2];
int index = 0;
for (var i = 0; i < characters.Length; i += 2)