URL Encoding/Decoding Tool

Frequently Asked Questions

What is URL encoding?

URL encoding is a method to convert 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 character's UTF-8 encoding. For example, the space character is encoded as %20. URLs cannot contain spaces, so they are replaced by either a plus (+) sign or %20. Other special characters are replaced with their corresponding %xx code.

Common characters that need URL encoding

The following are common characters that need URL encoding when used in URLs:

CharacterURL EncodedDescription
Space%20Most common character requiring URL encoding
!%21Exclamation mark
"%22Double quote
#%23Hash symbol (used for URL fragments)
$%24Dollar sign
%%25Percent sign (URL encoding escape character)
&%26Ampersand (used for URL parameter separation)
'%27Single quote
(%28Opening bracket
)%29Closing bracket
+%2BPlus sign
,%2CComma
/%2FForward slash (URL path separator)
=%3DEquals sign (URL parameter assignment)
?%3FQuestion mark (URL query string start)

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 is 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 /, ?, :, @, &, =, +, $, and #. In contrast, encodeURIComponent() encodes all special characters, making it suitable for encoding parts of a URL, such as query string parameters. When encoding query parameters, always use encodeURIComponent() to ensure all special characters are properly encoded.