How to Encode and Decode URLs - Complete Guide with Examples
Learn how to encode and decode URLs properly. Step-by-step guide with URL encoding formulas, percent-encoding examples, and best practices for web development.
Ready to try it?
Use our free URL Encoder & Decoder now — no signup required.
What is URL Encoding?
URL encoding is a method to convert special characters and non-ASCII characters into a safe format that can be transmitted over the internet. When URLs contain spaces, accented characters, or reserved symbols like &, =, or ?, these must be encoded to ensure proper transmission between web browsers and servers.
The encoding process replaces unsafe characters with a percent sign (%) followed by two hexadecimal digits representing the byte value in UTF-8 encoding. For example, a space becomes %20, and the ampersand (&) becomes %26. This process ensures that URLs remain valid and can be correctly interpreted by web servers.
URL decoding is the reverse process, converting percent-encoded characters back to their original form. This is essential when processing form data, query parameters, or any user input that was encoded before being included in a URL.
Formula and Methodology
The URL encoding formula follows the percent-encoding standard (RFC 3986):
Encoding Formula: Replace each unsafe character with % + 2-digit hex code
Step-by-step methodology:
- Identify characters that need encoding: spaces, special characters (<>#%{}|\^~[]`), non-ASCII characters (accents, emojis, CJK characters), and reserved characters (&;=+?$@!)
- Convert the character to its UTF-8 byte representation
- Convert each byte to a 2-digit hexadecimal number
- Prepend each hex value with %
Example conversions:
- Space (ASCII 32) → 0x20 → %20
- Ampersand (ASCII 38) → 0x26 → %26
- Equal sign (ASCII 61) → 0x3D → %3D
- Umlaut 'ü' (UTF-8: C3 BC) → %C3%BC
- Emoji '😀' (UTF-8: F0 9F 98 80) → %F0%9F%98%80
Real-World Examples
Example 1: Simple query parameter with spaces
Original URL: https://example.com/search?q=hello world
Encoded: https://example.com/search?q=hello%20world
Step-by-step: The space character (ASCII 32 = 0x20) is replaced with %20
Example 2: URL with special characters
Original URL: https://example.com/api?name=John&age=30&city=New York
Encoded: https://example.com/api?name=John%26age%3D30%26city%3DNew%20York
Step-by-step: & (ASCII 38 = 0x26) → %26, = (ASCII 61 = 0x3D) → %3D, space → %20
Example 3: International characters
Original: https://example.com/greet?message=Hello Café
Encoded: https://example.com/greet?message=Hello%20Caf%C3%A9
Step-by-step: 'é' in UTF-8 is bytes C3 A9, so it becomes %C3%A9
Common Mistakes to Avoid
1. Double encoding: Encoding an already encoded URL. For example, encoding %20 again produces %2520. Always check if your data is already encoded before applying encoding.
2. Encoding the entire URL: Only encode the query parameters and path segments, not the protocol, domain, or reserved characters that have structural meaning. Encoding the entire URL like https://example.com to %68%74%74%70%3A%2F%2F... breaks the URL structure.
3. Incorrect character set: Always use UTF-8 encoding. Using legacy encodings like ISO-8859-1 can cause mojibake (garbled text) when handling international characters.
4. Encoding reserved characters unnecessarily: Characters like / in path segments or ? in the query string separator should not be encoded unless they are part of the actual data value.
5. Forgetting to decode server-side: When receiving URL-encoded data on the server, remember to decode it before processing. Failing to decode means your database will store %20 instead of spaces.
Step-by-Step Guide
- 1
Gather Your Data
Collect the URL or string that needs encoding or decoding. Identify whether you need to encode (convert to safe format) or decode (convert back to readable format).
- 2
Enter Your Values
Input your URL or string into the encoder/decoder tool. For encoding, paste the raw URL with spaces and special characters. For decoding, paste the percent-encoded URL.
- 3
Calculate
Click the Encode or Decode button. The tool will process each character: encoding converts unsafe characters to %XX format, while decoding converts %XX sequences back to original characters.
- 4
Interpret Results
Review the output. For encoded results, verify that spaces became %20, special characters got proper percent-encoding, and international characters show multi-byte sequences like %C3%A9 for é.
- 5
Take Action
Use the encoded URL in your web requests, API calls, or form submissions. Use decoded text for display, database storage, or further processing. Always validate the result matches your expectations.
Tips & Best Practices
- lightbulb When encoding form data with multiple parameters, encode each value separately but leave the & and = separators unencoded to maintain query string structure
- lightbulb For URLs with international domains (IDN), use Punycode encoding (xn--) for the domain part, but UTF-8 percent-encoding for path and query parameters
- lightbulb JavaScript's encodeURIComponent() encodes more characters than encodeURI(). Use encodeURIComponent() for query parameters and encodeURI() for complete URLs
- lightbulb Avoid the pitfall of manually encoding URLs - always use built-in language functions like encodeURIComponent() in JavaScript, URLEncoder in Java, or urllib.parse.quote() in Python
- lightbulb For API development, remember that some characters like square brackets [] need encoding in query parameters for array notation: arr[]=1&arr[]=2 becomes arr%5B%5D=1%26arr%5B%5D=2
Frequently Asked Questions
What characters need to be URL encoded? expand_more
What is the difference between URL encoding and Base64 encoding? expand_more
Can URL encoding change the length of my URL? expand_more
Do I need to encode URLs in HTML attributes? expand_more
How do I handle URL encoding in different programming languages? expand_more
Related Tools
AI Compute Cost Analyzer
Estimate the cost of running AI workloads across various LLM providers and hardw...
AI Infrastructure Cost Calculator
Estimate monthly costs for LLM inference, GPU hosting, and vector database scali...
AI Prompt Complexity Analyzer
Analyze your AI prompts for token density, semantic complexity, and potential bi...
AI Prompt Cost Calculator
Calculate the cost of your LLM prompts based on token usage and model pricing....
AI Prompt Token Cost Calculator
Estimate the cost of your AI prompts across different LLM models like Claude, GP...