URL 인코딩/디코딩 도구
자주 묻는 질문
URL 인코딩이란 무엇인가요?
URL 인코딩은 문자를 인터넷을 통해 전송할 수 있는 형식으로 변환하는 방법입니다. URL은 ASCII 문자 집합만을 사용하여 인터넷을 통해 전송될 수 있습니다. URL은 종종 ASCII 집합 외부의 문자를 포함하기 때문에 변환이 필요합니다. URL 인코딩은 안전하지 않은 ASCII 문자를 '%' 기호와 두 개의 16진수로 대체합니다.
URL 인코딩은 어떻게 작동하나요?
URL 인코딩은 안전하지 않은 문자를 '%' 기호와 해당 문자의 UTF-8 인코딩을 나타내는 두 개의 16진수로 대체하는 방식으로 작동합니다. 예를 들어, 공백 문자는 %20으로 인코딩됩니다. URL에는 공백을 포함할 수 없으므로, 공백은 더하기 기호(+) 또는 %20으로 대체됩니다. 다른 특수 문자는 해당하는 %xx 코드로 대체됩니다.
URL 인코딩이 필요한 일반적인 문자
URL에서 사용할 때 URL 인코딩이 필요한 일반적인 문자는 다음과 같습니다:
문자 | URL 인코딩 | 설명 |
---|---|---|
Space | %20 | URL 인코딩이 필요한 가장 일반적인 문자 |
! | %21 | 느낌표 |
" | %22 | 큰따옴표 |
# | %23 | 해시 기호(URL 프래그먼트에 사용) |
$ | %24 | 달러 기호 |
% | %25 | 퍼센트 기호(URL 인코딩 이스케이프 문자) |
& | %26 | 앰퍼샌드(URL 매개변수 구분에 사용) |
' | %27 | 작은따옴표 |
( | %28 | 여는 괄호 |
) | %29 | 닫는 괄호 |
+ | %2B | 더하기 기호 |
, | %2C | 쉼표 |
/ | %2F | 슬래시(URL 경로 구분자) |
= | %3D | 등호(URL 매개변수 할당) |
? | %3F | 물음표(URL 쿼리 문자열 시작) |
다양한 프로그래밍 언어에서의 URL 인코딩 구현
다양한 프로그래밍 언어에서의 URL 인코딩 및 디코딩 예제는 다음과 같습니다:
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);
encodeURI와 encodeURIComponent의 차이점은 무엇인가요?
encodeURI()는 완전한 URI를 인코딩하도록 설계되었으므로, URL에서 특별한 의미를 가지는 문자(/, ?, :, @, &, =, +, $ 및 #)는 인코딩하지 않습니다. 반면에, encodeURIComponent()는 모든 특수 문자를 인코딩하므로, 쿼리 문자열 매개변수와 같은 URL의 일부를 인코딩하는 데 적합합니다. 쿼리 매개변수를 인코딩할 때는 모든 특수 문자가 올바르게 인코딩되도록 항상 encodeURIComponent()를 사용해야 합니다.