了解11种常见的字符编码格式及其使用场景,让数据传输不再出现问题

本文介绍了包括UTF-8、UTF-16、GBK、UTF-32、Base64、ASCII、ISO-8859-1、URL、HTML、Unicode和GBK在内的字符编码格式,提供了编码和解码的示例,并讨论了它们的使用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

🧑‍💻作者名称:DaenCode
🎤作者简介:啥技术都喜欢捣鼓捣鼓,喜欢分享技术、经验、生活。
😎人生感悟:尝尽人生百味,方知世间冷暖。


在这里插入图片描述


前言

在有一次SDK开发中,方法间数据传输出现了问题,由于返回数据采用的数据编码格式不一致会导致数据长度发生变化。因此整理出常用的字符编码格式,减少出现问题的概率。



🌟UTF-8编码

UTF-8 使用变长编码方式。最常用的字符编码之一,支持全球范围内的字符表示。每个字符的长度可以从 1 到 4 个字节不等。

代码示例

// 编码
String text = "Hello, world!";
byte[] encodedBytes = text.getBytes(StandardCharsets.UTF_8);
System.out.println(Arrays.toString(encodedBytes));
// 解码
String decodedText = new String(encodedBytes, StandardCharsets.UTF_8);
System.out.println(decodedText);  // Hello, world!

🌟UTF-16编码

定长编码方式,用于表示 Unicode 字符集。在 Java 中,字符串的内部表示采用 UTF-16 编码。

代码示例

// 编码
String text = "你好,世界!";
byte[] encodedBytes = text.getBytes(StandardCharsets.UTF_16);
System.out.println(Arrays.toString(encodedBytes));
// 解码
String decodedText = new String(encodedBytes, StandardCharsets.UTF_16);
System.out.println(decodedText); 

🌟GBK编码

双字节编码方式。中文字符集编码,支持汉字和其他中文字符。

代码示例

// 编码
String text = "你好,世界!";
byte[] encodedBytes = text.getBytes("GBK");
System.out.println(Arrays.toString(encodedBytes)); 
// 解码
String decodedText = new String(encodedBytes, "GBK");
System.out.println(decodedText); 

🌟UTF-32编码

定长编码方式,用于表示 Unicode 字符集。每个字符占用固定的 4 个字节空间,适用于对字符随机访问或精确控制字符长度的需求。

代码示例

// 编码
String text = "你好,世界!";
byte[] encodedBytes = text.getBytes(StandardCharsets.UTF_32);
System.out.println(Arrays.toString(encodedBytes));
// 解码
String decodedText = new String(encodedBytes, StandardCharsets.UTF_32);
System.out.println(decodedText);

🌟Base64编码

用于将二进制数据转换成可打印字符的编码方式

代码示例

// 编码
String text = "Hello, world!";
byte[] encodedBytes = Base64.getEncoder().encode(text.getBytes());
System.out.println(new String(encodedBytes));  // SGVsbG8sIHdvcmxkIQ==
// 解码
byte[] decodedBytes = Base64.getDecoder().decode(encodedBytes);
String decodedText = new String(decodedBytes);
System.out.println(decodedText);  // Hello, world!

🌟ASCII编码

最早的字符编码方式,用于表示基本的英文字母、数字和特殊字符。ASCII 编码使用一个字节表示一个字符,范围为 0-127。

代码示例

// 编码
String text = "Hello, world!";
byte[] encodedBytes = text.getBytes(StandardCharsets.US_ASCII);
System.out.println(Arrays.toString(encodedBytes)); 
// 解码
String decodedText = new String(encodedBytes, StandardCharsets.US_ASCII);
System.out.println(decodedText); 

🌟ISO-8859-1编码

也称为Latin-1编码,用于表示西欧语言字符集。ISO-8859-1 使用一个字节表示一个字符,范围为 0-255。

代码示例

// 编码
String text = "Hello, world!";
byte[] encodedBytes = text.getBytes(StandardCharsets.ISO_8859_1);
System.out.println(Arrays.toString(encodedBytes)); 
// 解码
String decodedText = new String(encodedBytes, StandardCharsets.ISO_8859_1);
System.out.println(decodedText); 

🌟URL编码

用于在 URL 中表示特殊字符和非 ASCII 字符。通过将特殊字符转换为 %xx 的形式,其中 xx 是字符的 ASCII 值的十六进制表示。

代码示例

// 编码
String text = "Hello, world!";
String encodedText = URLEncoder.encode(text, StandardCharsets.UTF_8.toString());
System.out.println(encodedText);  // Hello%2C+world%21
// 解码
String decodedText = URLDecoder.decode(encodedText, StandardCharsets.UTF_8.toString());
System.out.println(decodedText);  // Hello, world!

🌟HTML编码

用于在 HTML 中表示特殊字符和保留字符。将特殊字符转换为 &entity; 的形式,其中 entity 是特定字符的名称或编号。

代码示例

import org.apache.commons.text.StringEscapeUtils;
// 编码
String text = "<h1>Hello, world!</h1>";
String encodedText = StringEscapeUtils.escapeHtml4(text);
System.out.println(encodedText);  // &lt;h1&gt;Hello, world!&lt;/h1&gt;
// 解码
String decodedText = StringEscapeUtils.unescapeHtml4(encodedText);
System.out.println(decodedText);  // <h1>Hello, world!</h1>

🌟Unicode编码

Unicode 是一种字符集,定义了字符与码点之间的映射关系。在 Java 中,字符串使用 UTF-16 编码表示 Unicode 字符。

代码示例

// 编码
String text = "你好,世界!";
byte[] encodedBytes = text.getBytes(StandardCharsets.UTF_16);
System.out.println(Arrays.toString(encodedBytes));  
// 解码
String decodedText = new String(encodedBytes, StandardCharsets.UTF_16);
System.out.println(decodedText);

🌟GBK编码

Hex 编码是一种将字节数据转换为十六进制字符串的编码方式。它将每个字节转换为两个十六进制字符,从而以可读的方式表示二进制数据。

代码示例

import javax.xml.bind.DatatypeConverter;
// 编码
String text = "Hello, world!";
byte[] bytes = text.getBytes();
String encodedText = DatatypeConverter.printHexBinary(bytes);
System.out.println(encodedText);  // 48656C6C6F2C20776F726C6421
// 解码
byte[] decodedBytes = DatatypeConverter.parseHexBinary(encodedText);
String decodedText = new String(decodedBytes);
System.out.println(decodedText);  // Hello, world!

🌟使用场景

编码方式使用场景
UTF-8编码互联网文本传输和存储,多语言环境和国际化应用
UTF-16编码Windows操作系统,Java编程语言,多语言字符文本处理
GBK编码中文汉字编码和处理,中国大陆和台湾地区常见
UTF-32编码精确表示所有Unicode字符,不常用于存储空间敏感的场景
Base64编码二进制数据传输和存储,例如在电子邮件中传输附件
ASCII编码英文字符编码,常用于文本传输、数据存储、编程和键盘输入等场景。
ISO-8859-1编码常用于西欧语言环境下的文本处理和传输。
URL编码将URL中的非ASCII字符转换为%xx形式,保证传输和处理的正确性
HTML实体编码将HTML中的特殊字符转换为实体引用,避免与HTML标记冲突 ,常用于Web开发和网页显示中。
Unicode编码对全球范围字符的唯一标识,字符转换和互通的基础,用于处理多语言字符文本和实现Unicode字符的精确表示
Hex编码二进制数据转换为十六进制字符串,常用于调试和数据处理

🌟写在最后

以上就是11种常见的字符编码格式。文中若有错误或遗漏,望大家在评论区及时提出指正以及补充。感谢大家的阅读与指正。


请添加图片描述

评论 49
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DaenCode

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值