How to Fix the "Unexpected character code 32" Error
Character code 32 is an ordinary space. Learn why a parser rejects it, how to locate the stray space, and how to remove it without changing meaningful text.
An error such as "unexpected character code 32" means a parser, decoder or validator reached a plain space where its rules did not allow one. This guide explains what the message tells you, how to find the exact space, how to remove or encode it safely, and how to confirm the fix. No input sample, tool name or settings were supplied with this question, so the causes below are likely explanations to test, not confirmed diagnoses.
What the error message tells you
Character code 32 is the decimal value of the standard ASCII space, Unicode U+0020 (hexadecimal 20). It is the character produced by the space bar. The error does not mean your text is corrupted. It means a strict component, such as a JSON, URL, header, Base64 or configuration parser, expected a letter, digit, delimiter or end of input at a certain position and found a space instead.
The number narrows the search. A tab is code 9, a line feed is code 10, and a non-breaking space is code 160 (U+00A0). If your message says 32, look for an ordinary space, not an exotic invisible character. One caveat: some tools print codes in hexadecimal. In hexadecimal, 0x32 is the digit "2", not a space. Check your tool's documentation or nearby messages to confirm the base before you search.
| Code (decimal) | Hex | Character | How it usually appears |
| 32 | 0x20 | Space | Pressing the space bar; padding; copy-paste |
| 9 | 0x09 | Tab | Indentation from editors |
| 160 | 0xA0 | Non-breaking space | Text copied from web pages or word processors |
| 50 | 0x32 | Digit 2 | Only relevant if the tool reports hex |
Common causes to test
Each item below is a plausible cause. Without your actual input and output, none of them is confirmed. Match the description to where the error occurs, then use the checks in the next section to confirm.
- Leading or trailing spaces in a value, such as a token, ID, key or number pasted from another source.
- A space inside a field that must be continuous, such as a header name, an identifier, a hex string or a Base64 string.
- An unencoded space in a URL, query string or file path that is passed to a strict parser.
- A template, script or string concatenation that inserts a space between parts that should be joined directly.
- A configuration format that does not allow spaces around the separator, for example writing key = value where the parser expects key=value.
- A shell command where an unquoted variable or path containing a space was split into separate arguments, or where a space ended up inside a value.
Locate the exact space
Start with the position information in the error. Many parsers report a line, column or byte offset. If yours does, go straight to that location. If not, check the specific value the failing component reads, such as one header, one field or one config line, before you search the whole file.
On Linux or macOS, od prints each character so spaces become visible as gaps between escaped characters. The example below prints line 12 of a file. Replace the line number with the one from your error.
sed -n '12p' input.txt | od -c
For a hexadecimal view, xxd shows each space as 20, which is easy to scan for:
sed -n '12p' input.txt | xxd
To list every line that ends with one or more spaces, a frequent source of this error, use grep with an anchored pattern:
grep -nE ' +$' input.txt
If the error reports a character offset, a short Python snippet shows the surrounding characters with repr(), which wraps the string in quotes so leading and trailing spaces become obvious. The offset 57 is illustrative. Use the value from your error, and note whether your tool counts characters or bytes, because they differ for non-ASCII text.
text = open("input.txt", encoding="utf-8").read()
pos = 57 # example offset; use the one from your error
print(repr(text[max(0, pos - 15):pos + 15]))
print(ord(text[pos])) # prints 32 if the character at pos is a space
Most code editors can also display whitespace. Turning on a "render whitespace" or "show invisibles" option shows each space as a dot, so a stray space inside a value stands out.
Apply a targeted correction
Change only the space that causes the failure. A global "remove all spaces" step can break text where spaces carry meaning, such as sentences, names, file paths or values inside quoted strings. Choose the fix that matches the cause you confirmed.
- Stray leading or trailing space in a value: trim that value only, for example with strip() in Python or trim() in JavaScript, at the point where the value is read.
- Space inside a value that must be continuous, such as Base64 or hex: remove whitespace from that value only before decoding.
- Space in a URL or query string: percent-encode it as %20 (or + in form-encoded query strings) instead of deleting it, because the space may be part of the real name.
- Space in a shell argument or path: quote the variable or path, for example "$FILE", so it is passed as one argument.
- Spaces around a separator in a config file: rewrite the line in the format the parser documents, for example key=value instead of key = value, if that is what the format requires.
- Space introduced by a template or concatenation: fix the template source so later runs do not reintroduce it.
Example (illustrative, not tested against a specific tool): a header line written as "Content Type: text/plain" contains a space inside the header name, which header parsers commonly reject. The intended name is "Content-Type". The fix is to restore the hyphen, not to delete the space and produce "ContentType".
Before: Content Type: text/plain
After: Content-Type: text/plain
Example (illustrative): a Base64 value pasted with a line break replaced by a space, such as "SGVsbG8g d29ybGQ=", contains a space inside the encoded data. Removing the whitespace from that value alone gives "SGVsbG8gd29ybGQ=". Some decoders already ignore whitespace and others reject it, so check how yours behaves.
Confirm the issue is resolved
- Run the same command or request that produced the error, with the corrected input, and confirm the message no longer appears.
- Re-inspect the corrected location with od -c or xxd and confirm no 20 byte remains where the parser failed.
- Compare the original and corrected files, for example with diff original.txt corrected.txt, and check that only the intended space changed.
- If a different error now appears at a later position, repeat the location step. Input with one stray space often has others from the same source.
- If the input is generated, fix the generator or template and re-run it, so the corrected output does not depend on a manual edit.
When the checks do not find the space
If the location you inspected contains no space, the failing component may be reading different text from what you are looking at. Possibilities include a value assembled at runtime, an environment variable, a cached copy of the file, or input after an earlier transformation step. Log the exact string the parser receives, using repr() or an equivalent that makes whitespace visible, and inspect that. Also recheck whether the tool reports codes in hexadecimal, which would make the culprit the digit 2. To diagnose a specific case, the most useful details are the full error message, the name and version of the tool that produced it, and a sample of the input around the reported position.