الأسئلة الشائعة
ما هو ترميز UTF-8؟
UTF-8 هو ترميز حرفي متعدد الأطوال لليونيكود. يستخدم 1 إلى 4 بايت لتمثيل الحروف، مما يجعله فعالًا في ترميز أحرف ASCII بينما يدعم جميع أحرف يونيكود.
كيف يحوّل هذه الأداة النص إلى UTF-8؟
تستخدم هذه الأداة TextEncoder المدمج في المتصفح لترميز النص إلى UTF-8. يتم تحويل كل حرف إلى بايت واحد أو أكثر بناءً على نقطة الكود الخاصة به، ثم يتم تنسيقه كـ تسلسلات هروب هيكساديسيمالية (على سبيل المثال، \xE4\xB8\xAD تمثل '中').
كيف يحوّل هذه الأداة UTF-8 إلى نص؟
تزيل الأداة بادئة \x من المدخلات وتحلل القيم الهيكساديسيمالية المتبقية إلى بايتات. ثم يتم فك تشفير هذه البايتات إلى نص باستخدام TextDecoder في المتصفح، مع إعادة بناء الحروف الأصلية وفقًا لقواعد ترميز UTF-8.
لماذا يستخدم UTF-8 على نطاق واسع؟
يستخدم UTF-8 على نطاق واسع لأنه متوافق مع ASCII، وفعال في ترميز النصوص الإنجليزية، وقادر على ترميز جميع أحرف يونيكود. وهو الترميز الافتراضي لصفحات الويب والعديد من الأنظمة الأخرى، مما يضمن التناسق عبر المنصات.
ما هي مبادئ ترميز UTF-8؟
يعمل ترميز UTF-8 عن طريق تجميع نقاط كود يونيكود وترميزها إلى تسلسلات بايت:
- النقاط من U+0000 إلى U+007F يتم ترميزها في بايت واحد (متوافق مع ASCII).
- النقاط من U+0080 إلى U+07FF يتم ترميزها في بايتين.
- النقاط من U+0800 إلى U+FFFF يتم ترميزها في ثلاث بايتات.
- النقاط من U+10000 إلى U+10FFFF يتم ترميزها في أربع بايتات.
يبدأ كل بايت في تسلسل متعدد البايتات بنمط بت محدد للإشارة إلى دوره في التسلسل، مما يضمن أن 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);