UTF-8 변환 도구

자주 묻는 질문

UTF-8이란 무엇인가요?

UTF-8은 Unicode를 위한 가변 길이 문자 인코딩입니다. 문자마다 1~4바이트를 사용하여 표현하며, ASCII 문자 인코딩에는 효율적이고 모든 Unicode 문자를 지원합니다.

이 도구는 텍스트를 어떻게 UTF-8로 변환하나요?

이 도구는 브라우저에 내장된 TextEncoder를 사용하여 텍스트를 UTF-8로 인코딩합니다. 각 문자는 해당 Unicode 코드 포인트에 따라 하나 이상의 바이트로 변환되며, 이후 \xE4\xB8\xAD와 같은 16진수 이스케이프 시퀀스로 표시됩니다 (예: '中').

이 도구는 UTF-8을 어떻게 텍스트로 변환하나요?

이 도구는 입력에서 \x 접두사를 제거한 후, 나머지 16진수 값을 바이트로 분석합니다. 이 바이트들은 브라우저의 TextDecoder를 통해 UTF-8 규칙에 따라 디코딩되어 원래의 문자를 복원합니다.

UTF-8이 널리 사용되는 이유는 무엇인가요?

UTF-8은 ASCII와의 하위 호환성이 있고, 영어 텍스트에 대한 인코딩이 효율적이며, 모든 Unicode 문자를 인코딩할 수 있어 널리 사용됩니다. 웹 페이지 및 다양한 시스템의 기본 인코딩이기 때문에 플랫폼 간 텍스트 일관성을 보장합니다.

UTF-8 인코딩의 원리는 무엇인가요?

UTF-8 인코딩은 Unicode 코드 포인트를 바이트 시퀀스로 변환하는 방식으로 동작합니다:

  • U+0000부터 U+007F까지의 코드 포인트는 1바이트로 인코딩됩니다 (ASCII와 호환).
  • U+0080부터 U+07FF까지의 코드 포인트는 2바이트로 인코딩됩니다.
  • U+0800부터 U+FFFF까지의 코드 포인트는 3바이트로 인코딩됩니다.
  • U+10000부터 U+10FFFF까지의 코드 포인트는 4바이트로 인코딩됩니다.

여러 바이트로 구성된 시퀀스에서 각 바이트는 특정 비트 패턴으로 시작하여 시퀀스 내의 위치를 나타내므로, UTF-8은 자체 동기화 기능과 오류 복원력을 갖추고 있습니다.

다양한 프로그래밍 언어에서 UTF-8 변환을 구현하는 방법은?

아래는 문자열을 UTF-8 바이트로 인코딩하고, UTF-8 바이트를 다시 문자열로 디코딩하는 방법을 보여주는 여러 언어의 예시입니다:

Go

Go 예제 코드: UTF-8 변환


import "fmt"

func main() {
    text := "Hello, World!"
    // Encode string to UTF-8 bytes
    utf8Bytes := []byte(text)
    fmt.Printf("UTF-8 bytes: %x\n", utf8Bytes)

    // Decode UTF-8 bytes back to string
    decodedText := string(utf8Bytes)
    fmt.Printf("Decoded text: %s\n", decodedText)
}
      
Java

Java 예제 코드: UTF-8 변환


import java.nio.charset.StandardCharsets;

public class Utf8Example {
    public static void main(String[] args) {
        String text = "Hello, World!";
        // Encode string to UTF-8 bytes
        byte[] utf8Bytes = text.getBytes(StandardCharsets.UTF_8);
        System.out.println("UTF-8 bytes: " + java.util.Arrays.toString(utf8Bytes));

        // Decode UTF-8 bytes back to string
        String decodedText = new String(utf8Bytes, StandardCharsets.UTF_8);
        System.out.println("Decoded text: " + decodedText);
    }
}
      
Python

Python 예제 코드: UTF-8 변환


text = "Hello, World!"
# Encode string to UTF-8 bytes
utf8_bytes = text.encode("utf-8")
print(f"UTF-8 bytes: {utf8_bytes}")

# Decode UTF-8 bytes back to string
decoded_text = utf8_bytes.decode("utf-8")
print(f"Decoded text: {decoded_text}")
      
PHP

PHP 예제 코드: UTF-8 변환


<?php
$text = "Hello, World!";
// Encode string to UTF-8 bytes
$utf8Bytes = utf8_encode($text);
echo "UTF-8 bytes: " . bin2hex($utf8Bytes) . PHP_EOL;

// Decode UTF-8 bytes back to string
$decodedText = utf8_decode($utf8Bytes);
echo "Decoded text: " . $decodedText . PHP_EOL;
?>
      
JavaScript

JavaScript 예제 코드: UTF-8 변환


const text = "Hello, World!";
// Encode string to UTF-8 bytes
const encoder = new TextEncoder();
const utf8Bytes = encoder.encode(text);
console.log("UTF-8 bytes:", Array.from(utf8Bytes));

// Decode UTF-8 bytes back to string
const decoder = new TextDecoder("utf-8");
const decodedText = decoder.decode(utf8Bytes);
console.log("Decoded text:", decodedText);
      
TypeScript

TypeScript 예제 코드: UTF-8 변환


const text: string = "Hello, World!";
// Encode string to UTF-8 bytes
const encoder: TextEncoder = new TextEncoder();
const utf8Bytes: Uint8Array = encoder.encode(text);
console.log("UTF-8 bytes:", Array.from(utf8Bytes));

// Decode UTF-8 bytes back to string
const decoder: TextDecoder = new TextDecoder("utf-8");
const decodedText: string = decoder.decode(utf8Bytes);
console.log("Decoded text:", decodedText);