{"record":{"id":"e75a6b1f09d98d4d","repo":"zloirock/core-js","slug":"string-should-be-an-even-number-of-characters","errorCode":null,"errorMessage":"String should be an even number of characters","messagePattern":"String should be an even number of characters","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"packages/core-js/internals/uint8-from-hex.js","lineNumber":12,"sourceCode":"'use strict';\nvar globalThis = require('../internals/global-this');\nvar uncurryThis = require('../internals/function-uncurry-this');\n\nvar Uint8Array = globalThis.Uint8Array;\nvar SyntaxError = globalThis.SyntaxError;\nvar min = Math.min;\nvar stringMatch = uncurryThis(''.match);\n\nmodule.exports = function (string, into) {\n  var stringLength = string.length;\n  if (stringLength % 2 !== 0) throw new SyntaxError('String should be an even number of characters');\n  var maxLength = into ? min(into.length, stringLength / 2) : stringLength / 2;\n  var bytes = into || new Uint8Array(maxLength);\n  var segments = stringMatch(string, /[\\S\\s]{2}/g);\n  var written = 0;\n  for (; written < maxLength; written++) {\n    var result = +('0x' + segments[written] + '0');\n    // eslint-disable-next-line no-self-compare -- NaN check\n    if (result !== result) {\n      throw new SyntaxError('String should only contain hex characters');\n    }\n    bytes[written] = result >> 4;\n  }\n  return { bytes: bytes, read: written << 1 };\n};\n","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/zloirock/core-js/blob/84e45fba098dd3a177d5cf2247d06ab8e98d3790/packages/core-js/internals/uint8-from-hex.js#L1-L27","documentation":"Uint8Array.fromHex() requires the input to be a hex string with an even number of characters, since every byte is exactly two hex digits. If string.length % 2 !== 0 (a nibble is missing), decoding is ambiguous and the library throws this SyntaxError before any bytes are written.","triggerScenarios":"Calling Uint8Array.fromHex('abc') or fromHexInto with any odd-length string — e.g. a hex string missing its last character, produced by a '%x' format that dropped a leading zero ('0x5' instead of '05' concatenated), or manually built nibble-by-nibble.","commonSituations":"Formatting individual bytes with '0x%x' without zero-padding so '0x0A' becomes 'a', string truncation by fixed-length buffers, copy/paste dropping a character, or concatenating hex nibbles instead of byte pairs.","solutions":["Pad the string to even length with a leading '0' if a single leading nibble was dropped: s.length % 2 ? '0' + s : s.","Fix the producer to zero-pad each byte to two hex digits (e.g. n.toString(16).padStart(2, '0')).","Verify the string wasn't truncated in storage/transport; compare length to the expected byteCount * 2.","Catch SyntaxError at the boundary and report 'odd-length hex string' to the caller."],"exampleFix":"// before\nUint8Array.fromHex('4a617'); // SyntaxError: String should be an even number of characters\n// after\nconst hex = raw.length % 2 === 0 ? raw : '0' + raw;\nconst bytes = Uint8Array.fromHex(hex);","handlingStrategy":"validation","validationCode":"function isEvenLengthHex(s) {\n  return typeof s === 'string' && /^[0-9a-fA-F]*$/.test(s) && s.length % 2 === 0;\n}\n// if (!isEvenLengthHex(input)) input = input.padStart(input.length + (input.length % 2), '0');","typeGuard":"function isHexString(s) {\n  return typeof s === 'string' && s.length % 2 === 0 && /^[0-9a-fA-F]+$/.test(s);\n}","tryCatchPattern":"try {\n  const bytes = Uint8Array.fromHex(input);\n} catch (e) {\n  if (e instanceof SyntaxError && e.message.includes('even number of characters')) {\n    throw new TypeError(`Hex string must have even length, got ${input.length}`);\n  }\n  throw e;\n}","preventionTips":["Zero-pad hex bytes at the source: n.toString(16).padStart(2, '0').","Avoid '0x' prefixes leaking into concatenated hex strings.","Validate length === expectedByteCount * 2 before decoding.","Guard copy/paste and truncation by checking even length at the input boundary."],"tags":["hex","syntax-error","validation","encoding"],"backgroundTag":"odd-length-hex-string","analyzedSha":"84e45fba098dd3a177d5cf2247d06ab8e98d3790","analyzedAt":"2026-08-30T20:36:10.323Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}