您可能想查看 UTF-8 算法,因为它完全符合您的要求。它存储大量数据,同时丢弃零、保留相关数据并对其进行编码以占用更少的磁盘空间。
适用于:Java 版本 7+
import java.nio.charset.StandardCharsets;
import java.util.Formatter;
public class UTF8EncodeDecode {
public static byte[] utf8encode(int codepoint) {
return new String(new int[]{codepoint}, 0, 1).getBytes(StandardCharsets.UTF_8);
}
public static int utf8decode(byte[] bytes) {
return new String(bytes, StandardCharsets.UTF_8).codePointAt(0);
}
public static void main(String[] args) {
System.out.printf("%-7s %-43s %7s\t%s\t%7s%n",
"Char", "Name", "Unicode", "UTF-8 encoded", "Decoded");
for (int codepoint : new int[]{0x0041, 0x00F6, 0x0416, 0x20AC, 0x1D11E}) {
byte[] encoded = utf8encode(codepoint);
Formatter formatter = new Formatter();
for (byte b : encoded) {
formatter.format("%02X ", b);
}
String encodedHex = formatter.toString();
int decoded = utf8decode(encoded);
System.out.printf("%-7c %-43s U+%04X\t%-12s\tU+%04X%n",
codepoint, Character.getName(codepoint), codepoint, encodedHex, decoded);
}
}
}
https://rosettacode.org/wiki/UTF-8_encode_and_decode#Java
UTF-8 是一种可变宽度字符编码,能够使用一到四个 8 位字节对 Unicode 中的所有 1,112,064[nb 1] 个有效代码点进行编码。[nb 2] 该编码由 Unicode 标准定义,并且是最初由 Ken Thompson 和 Rob Pike 设计。[1][2]该名称源自 Unicode(或通用编码字符集)转换格式 – 8 位。[3]
它是为向后兼容 ASCII 而设计的。具有较低数值的代码点往往更频繁地出现,使用较少的字节进行编码。 Unicode 的前 128 个字符与 ASCII 一对一对应,使用与 ASCII 具有相同二进制值的单个字节进行编码,因此有效的 ASCII 文本也是有效的 UTF-8 编码的 Unicode。由于在将非 ASCII 码位编码为 UTF-8 时不会出现 ASCII 字节,因此在大多数以特殊方式解释某些 ASCII 字符的编程和文档语言中使用 UTF-8 是安全的,例如“/”(斜杠)文件名、转义序列中的“\”(反斜杠)和 printf 中的“%”。
https://en.wikipedia.org/wiki/UTF-8
二进制 11110000 10010000 10001101 10001000 在 UTF-8 中变为 F0 90 8D 88。由于您将其存储为文本,因此您从必须存储 32 个字符变为存储 8 个字符。而且由于它是一种众所周知且设计良好的编码,您可以轻松地对其进行反转。所有的数学都是为你完成的。
00010010100010101000100100(或者更确切地说是00000001 0010100 0101010 00100100)的示例转换为*$(我的机器上的两个不可打印字符)。这就是二进制文件的 UTF-8 编码。我错误地使用了另一个网站,该网站使用我输入的十进制数据而不是二进制数据。
https://onlineutf8tools.com/convert-binary-to-utf8
对于 UTF-8 以及它如何应用于答案的一个非常好的解释:
https://hackaday.com/2013/09/27/utf-8-the-most-elegant-hack/
编辑:
我认为这个问题是为了减少存储值所需的字符数量,这是一种编码。 UTF-8 是一种编码。以“非标准”方式使用,OP 可以使用 UTF-8 以更短的格式对其 0 和 1 的字符串进行编码。这就是这个答案的相关性。
如果将字符连接起来,则可以轻松地从 4x 8 位(32 位)变为 8x 8 位(64 位),并编码一个大至 9,223,372,036,854,775,807 的值。