{"record":{"id":"6c94273e412a1418","repo":"clockworklabs/SpacetimeDB","slug":"uint8array-is-not-32-bytes-long-array","errorCode":null,"errorMessage":"Uint8Array is not 32 bytes long: [${array}]","messagePattern":"Uint8Array is not 32 bytes long: \\[(.+?)\\]","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"crates/bindings-typescript/src/lib/util.ts","lineNumber":55,"sourceCode":"  return true;\n}\n\nexport function uint8ArrayToHexString(array: Uint8Array): string {\n  return Array.prototype.map\n    .call(array.reverse(), x => ('00' + x.toString(16)).slice(-2))\n    .join('');\n}\n\nexport function uint8ArrayToU128(array: Uint8Array): bigint {\n  if (array.length != 16) {\n    throw new Error(`Uint8Array is not 16 bytes long: ${array}`);\n  }\n  return new BinaryReader(array).readU128();\n}\n\nexport function uint8ArrayToU256(array: Uint8Array): bigint {\n  if (array.length != 32) {\n    throw new Error(`Uint8Array is not 32 bytes long: [${array}]`);\n  }\n  return new BinaryReader(array).readU256();\n}\n\nexport function hexStringToUint8Array(str: string): Uint8Array {\n  if (str.startsWith('0x')) {\n    str = str.slice(2);\n  }\n  const matches = str.match(/.{1,2}/g) || [];\n  const data = Uint8Array.from(\n    matches.map((byte: string) => parseInt(byte, 16))\n  );\n  return data.reverse();\n}\n\nexport function hexStringToU128(str: string): bigint {\n  return uint8ArrayToU128(hexStringToUint8Array(str));\n}","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/524b4487d949b61a07d4f39c862d1290259dfd20/crates/bindings-typescript/src/lib/util.ts#L37-L73","documentation":"uint8ArrayToU256 interprets the bytes as a little-endian 256-bit integer via BinaryReader.readU256, so exactly 32 bytes are required; any other length throws. It backs hexStringToU256, so hex input decoding to the wrong byte count also lands here.","triggerScenarios":"Passing a Uint8Array whose length is not 32 directly, or a hex string that decodes to a non-32-byte array - e.g. a 16-byte u128 hex (32 chars) fed to the u256 helper.","commonSituations":"Confusing 128-bit and 256-bit id widths; slicing with wrong offsets or lengths; hex strings with odd length or missing padding.","solutions":["Validate array.length === 32 before calling","Check the source hex string: after stripping '0x' it must be exactly 64 hex chars","Use uint8ArrayToU128 for 16-byte values"],"exampleFix":"// before\nconst v = uint8ArrayToU256(hexStringToUint8Array('0x' + 'ab'.repeat(16))); // 16 bytes -> throws\n\n// after\nconst v = uint8ArrayToU256(hexStringToUint8Array('0x' + 'ab'.repeat(32))); // exactly 32 bytes","handlingStrategy":"validation","validationCode":"function isU256Bytes(array: Uint8Array): boolean {\n  return array.length === 32;\n}\n// if (!isU256Bytes(bytes)) throw new TypeError(`expected 32 bytes, got ${bytes.length}`);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check length before converting; 32 bytes = u256, 16 bytes = u128","Validate hex strings: 64 hex chars (after stripping 0x) for u256","Name variables after their width (id128/id256) to avoid mixing them up"],"tags":["bytes","u256","conversion","javascript","validation"],"backgroundTag":"fixed-width-byte-length-mismatch","analyzedSha":"524b4487d949b61a07d4f39c862d1290259dfd20","analyzedAt":"2026-08-16T23:58:54.611Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}