{"record":{"id":"5bbc72e953fed5d2","repo":"zloirock/core-js","slug":"string-should-only-contain-hex-characters","errorCode":null,"errorMessage":"String should only contain hex characters","messagePattern":"String should only contain hex characters","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"packages/core-js/internals/uint8-from-hex.js","lineNumber":21,"sourceCode":"var 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":3,"sourceCodeEnd":27,"githubUrl":"https://github.com/zloirock/core-js/blob/84e45fba098dd3a177d5cf2247d06ab8e98d3790/packages/core-js/internals/uint8-from-hex.js#L3-L27","documentation":"This SyntaxError comes from the core-js polyfill for Uint8Array.fromHex. It means the input string contained a pair of characters that, when interpreted as a hexadecimal byte literal ('0x' + pair + '0'), did not parse to a number (NaN), i.e. the string contains non-hex characters. The polyfill deliberately rejects such input because the spec requires hex-only strings.","triggerScenarios":"Calling Uint8Array.fromHex() (or the underlying fromHex function in uint8-from-hex.js) with a string containing any characters outside [0-9a-fA-F], or a string whose length is not a multiple of two so the /([\\S\\s]{2})/g segmentation pairs a hex digit with a non-hex trailing character.","commonSituations":"Passing base64 or base64url data to fromHex instead of fromBase64; hex strings copied from logs that include '0x' prefixes, spaces, colons ('aa:bb:cc'), or whitespace between bytes; user-supplied input that was never validated; uppercase '0X' prefix left on the string.","solutions":["Strip non-hex characters and prefixes before the call: str = str.replace(/^0x/, '').replace(/[^0-9a-fA-F]/g, '')","Validate the string first with /^(?:[0-9a-fA-F]{2})+$/ before calling Uint8Array.fromHex","If the data is not actually hex, use Uint8Array.fromBase64 (core-js also polyfills it) or atob instead","Ensure the cleaned string has even length; pad with a leading '0' if a nibble was dropped"],"exampleFix":"// before\nconst bytes = Uint8Array.fromHex('0xdeadbeef');\n// after\nconst hex = '0xdeadbeef'.replace(/^0x/, '');\nif (!/^(?:[0-9a-fA-F]{2})+$/.test(hex)) throw new Error('invalid hex');\nconst bytes = Uint8Array.fromHex(hex);","handlingStrategy":"validation","validationCode":"function isValidHex(s) {\n  return typeof s === 'string' && /^(?:[0-9a-fA-F]{2})+$/.test(s);\n}\nif (!isValidHex(input)) throw new Error('input must be an even-length hex string');\nconst bytes = Uint8Array.fromHex(input);","typeGuard":"function isHexString(v) {\n  return typeof v === 'string' && v.length % 2 === 0 && /^[0-9a-fA-F]+$/.test(v);\n}","tryCatchPattern":"let bytes;\ntry {\n  bytes = Uint8Array.fromHex(input);\n} catch (e) {\n  if (e instanceof SyntaxError && /hex characters/.test(e.message)) {\n    bytes = Uint8Array.fromHex(input.replace(/[^0-9a-fA-F]/g, ''));\n  } else throw e;\n}","preventionTips":["Always validate user-supplied hex with a regex before parsing","Strip '0x' prefixes and separators (spaces, colons, dashes) at the boundary where the string enters your system","Use Uint8Array.fromBase64 for base64 data, not fromHex","Keep hex strings lowercase and even-length by convention"],"tags":["syntax-error","hex-parsing","uint8array","polyfill","validation"],"backgroundTag":"invalid-hex-string","analyzedSha":"84e45fba098dd3a177d5cf2247d06ab8e98d3790","analyzedAt":"2026-08-30T20:36:10.323Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}