From: Volker Mische Date: Thu, 18 Jan 2018 23:44:15 +0000 (+0100) Subject: Hex strings no longer need have even number of digits X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=68c2a6ea1d909b0e37a641fe2f9831d5168fe34f;p=buffer.git Hex strings no longer need have even number of digits If a hex string has an odd number of bytes, the last byte is just ignored in Node.js. --- diff --git a/index.js b/index.js index 6944c05..a6d094a 100644 --- a/index.js +++ b/index.js @@ -752,9 +752,7 @@ function hexWrite (buf, string, offset, length) { } } - // must be an even number of digits var strLen = string.length - if (strLen % 2 !== 0) throw new TypeError('Invalid hex string') if (length > strLen / 2) { length = strLen / 2 diff --git a/test/node/test-buffer-alloc.js b/test/node/test-buffer-alloc.js index c813a23..dd4799c 100644 --- a/test/node/test-buffer-alloc.js +++ b/test/node/test-buffer-alloc.js @@ -516,13 +516,13 @@ assert.deepStrictEqual(Buffer.from(' YWJvcnVtLg', 'base64'), assert.strictEqual(hexb2[i], hexb[i]); } } -/* + // Test single hex character is discarded. assert.strictEqual(Buffer.from('A', 'hex').length, 0); // Test that if a trailing character is discarded, rest of string is processed. assert.deepStrictEqual(Buffer.from('Abx', 'hex'), Buffer.from('Ab', 'hex')); -*/ + // Test single base64 char encodes as 0. assert.strictEqual(Buffer.from('A', 'base64').length, 0);