URL Encoding/Decoding Tool
Frequently Asked Questions
What is URL Encoding?
URL encoding is a method of converting characters into a format that can be transmitted over the Internet. URLs can only be sent over the Internet using the ASCII character set. Since URLs often contain characters outside the ASCII set, they need to be converted. URL encoding replaces unsafe ASCII characters with a '%' followed by two hexadecimal digits.
How Does URL Encoding Work?
URL encoding works by replacing unsafe characters with a '%' followed by two hexadecimal digits representing the UTF-8 encoding of that character. For example, the space character is encoded as %20. URLs cannot contain spaces, so they are replaced with either a plus sign (+) or %20. Other special characters are replaced with their corresponding %xx codes.
Common Characters That Need URL Encoding
The following are common characters that need URL encoding when used in URLs:
Character | URL Encoded | Description |
---|---|---|
Space | %20 | Most common character that requires URL encoding |
! | %21 | Exclamation mark |
" | %22 | Double quote |
# | %23 | Hash symbol (used for URL fragments) |
$ | %24 | Dollar sign |
% | %25 | Percent sign (URL encoding escape character) |
& | %26 | Ampersand (used for URL parameter separation) |
' | %27 | Single quote |
( | %28 | Opening parenthesis |
) | %29 | Closing parenthesis |
* | %2A | Asterisk |
+ | %2B | Plus sign |
, | %2C | Comma |
/ | %2F | Forward slash (URL path separator) |
: | %3A | Colon |
; | %3B | Semicolon |
= | %3D | Equals sign (URL parameter assignment) |
? | %3F | Question mark (URL query string start) |
@ | %40 | At symbol |
[ | %5B | Opening square bracket |
\ | %5C | Backslash |
] | %5D | Closing square bracket |
^ | %5E | Caret |
{ | %7B | Opening curly brace |
| | %7C | Vertical bar |
} | %7D | Closing curly brace |
~ | %7E | Tilde |
URL Encoding Implementation in Different Programming Languages
Here are examples of URL encoding and decoding in various programming languages:
Go
package main
import (
"fmt"
"net/url"
)
func main() {
// Encode a URL
text := "Hello World! Special chars: &?=/";
encoded := url.QueryEscape(text)
fmt.Println("Encoded:", encoded)
// Decode a URL
decoded, err := url.QueryUnescape(encoded)
if err == nil {
fmt.Println("Decoded:", decoded)
}
}
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// Function to URL-encode a string
char *url_encode(char *str) {
char *encoded = malloc(strlen(str) * 3 + 1);
char *pstr = str;
char *pbuf = encoded;
while (*pstr) {
if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') {
*pbuf++ = *pstr;
} else if (*pstr == ' ') {
*pbuf++ = '+';
} else {
sprintf(pbuf, "%%%.2X", *pstr);
pbuf += 3;
}
pstr++;
}
*pbuf = '\\0';
return encoded;
}
int main() {
char *text = "Hello World! Special chars: &?=/";
char *encoded = url_encode(text);
printf("Original: %s\\n", text);
printf("Encoded: %s\\n", encoded);
free(encoded);
return 0;
}
PHP
<?php
// URL encoding
$text = "Hello World! Special chars: &?=/";
$encoded = urlencode($text);
echo "Encoded: " . $encoded . "\\n";
// URL decoding
$decoded = urldecode($encoded);
echo "Decoded: " . $decoded . "\\n";
?>
Python
import urllib.parse
# URL encoding
text = "Hello World! Special chars: &?=/"
encoded = urllib.parse.quote(text)
print(f"Encoded: {encoded}")
# URL decoding
decoded = urllib.parse.unquote(encoded)
print(f"Decoded: {decoded}")
JavaScript
// URL encoding
const text = "Hello World! Special chars: &?=/";
const encoded = encodeURIComponent(text);
console.log("Encoded:", encoded);
// URL decoding
const decoded = decodeURIComponent(encoded);
console.log("Decoded:", decoded);
TypeScript
// URL encoding
const text: string = "Hello World! Special chars: &?=/";
const encoded: string = encodeURIComponent(text);
console.log("Encoded:", encoded);
// URL decoding
const decoded: string = decodeURIComponent(encoded);
console.log("Decoded:", decoded);
What's the Difference Between encodeURI and encodeURIComponent?
encodeURI() is designed to encode a complete URI, so it does not encode characters that have special meaning in a URL, such as /, ?, :, \u0040, \u0026, =, +, $, and #. encodeURIComponent(), on the other hand, encodes every character that has special meaning, making it ideal for encoding individual query string parameters. When encoding URL parameters, always use encodeURIComponent() to ensure all special characters are properly encoded.