{"record":{"id":"540f36424f479e0a","repo":"ramsey/uuid","slug":"bytes-string-should-contain-16-characters-540f36","errorCode":null,"errorMessage":"$bytes string should contain 16 characters.","messagePattern":"\\$bytes string should contain 16 characters\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Codec/StringCodec.php","lineNumber":86,"sourceCode":"        /** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */\n        return $uuid->getFields()->getBytes();\n    }\n\n    /**\n     * @throws InvalidUuidStringException\n     *\n     * @inheritDoc\n     */\n    public function decode(string $encodedUuid): UuidInterface\n    {\n        /** @phpstan-ignore possiblyImpure.methodCall */\n        return $this->builder->build($this, $this->getBytes($encodedUuid));\n    }\n\n    public function decodeBytes(string $bytes): UuidInterface\n    {\n        if (strlen($bytes) !== 16) {\n            throw new InvalidArgumentException('$bytes string should contain 16 characters.');\n        }\n\n        return $this->builder->build($this, $bytes);\n    }\n\n    /**\n     * Returns the UUID builder\n     */\n    protected function getBuilder(): UuidBuilderInterface\n    {\n        return $this->builder;\n    }\n\n    /**\n     * Returns a byte string of the UUID\n     */\n    protected function getBytes(string $encodedUuid): string\n    {","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/ramsey/uuid/blob/da5b521600a707d2dd097598464bd3090de850f5/src/Codec/StringCodec.php#L68-L104","documentation":"StringCodec::decodeBytes() is the standard binary decode path (used by Uuid::fromBytes()); it builds a UUID from a raw byte string and throws InvalidArgumentException when strlen($bytes) !== 16. The bytes must be the plain 16-octet RFC 4122 representation with no encoding or formatting applied.","triggerScenarios":"Uuid::fromBytes($value) where $value is a 32-char hex string, a hyphenated UUID string, base64, or a truncated/oversized binary value.","commonSituations":"Column stored as CHAR(36)/hex instead of BINARY(16); forgetting hex2bin() after hex transport; ORM or DBAL returning a formatted string for binary columns; substr() bugs cutting bytes.","solutions":["Convert first: (string) hex2bin($hex) / base64_decode($b64), then check strlen === 16.","Pre-validate length before fromBytes()/decodeBytes().","Use BINARY(16) storage so values arrive as exactly 16 raw bytes."],"exampleFix":"// before\n$uuid = Uuid::fromBytes($hexFromDb); // 32 hex chars -> InvalidArgumentException\n\n// after\n$bytes = (string) hex2bin($hexFromDb);\n$uuid = Uuid::fromBytes($bytes); // strlen === 16","handlingStrategy":"validation","validationCode":"function normalizeTo16Bytes(string $value): string\n{\n    if (strlen($value) === 32 && ctype_xdigit($value)) {\n        $value = (string) hex2bin($value);\n    }\n    if (strlen($value) !== 16) {\n        throw new InvalidArgumentException(sprintf('expected 16 bytes, got %d', strlen($value)));\n    }\n\n    return $value;\n}\n\n$uuid = Uuid::fromBytes(normalizeTo16Bytes($value));","typeGuard":"function isUuidByteString(string $value): bool\n{\n    return strlen($value) === 16;\n}","tryCatchPattern":"try {\n    $uuid = Uuid::fromBytes($bytes);\n} catch (\\Ramsey\\Uuid\\Exception\\InvalidArgumentException $e) {\n    throw new StoredIdentifierCorruptException($row['id'], $e);\n}","preventionTips":["Use BINARY(16) schema columns so drivers return exactly 16 raw bytes.","Convert hex/base64 at the boundary, never mid-stack.","Wrap fromBytes() in one helper with the length assert to keep call sites safe.","Watch DBAL type mappings - some map BINARY(16) to hex strings by default."],"tags":["php","ramsey-uuid","string-codec","decode-bytes","length-check"],"backgroundTag":"uuid-bytes-length-invalid","analyzedSha":"da5b521600a707d2dd097598464bd3090de850f5","analyzedAt":"2026-08-21T01:35:29.252Z","schemaVersion":2},"datasetVersion":"2026-08-21T03:17:12.404Z"}