将一串位转换为 java 中的 unicode 字符
Convert a string of bits to unicode character in java
我正在尝试将一串位转换为 java 中的 Unicode 字符。问题是我只得到中文符号等
字符串位 = "01010011011011100110000101110010"
有人知道怎么做吗?
值 <= 32 位
使用Integer.parseInt
解析二进制字符串,然后将其转换为字节数组(使用ByteBuffer
),最后将字节数组转换为String
:
String bits = "01010011011011100110000101110010"
new String(
ByteBuffer.allocate(4).putInt(
Integer.parseInt(bits, 2)
).array(),
StandardCharsets.UTF_8
);
值 > 32 位
对于任意大的 bits
字符串,您也可以使用 BigInteger
:
new String(
new BigInteger(bits, 2).toByteArray(),
StandardCharsets.UTF_8
);
结果
Snar
我正在尝试将一串位转换为 java 中的 Unicode 字符。问题是我只得到中文符号等
字符串位 = "01010011011011100110000101110010"
有人知道怎么做吗?
值 <= 32 位
使用Integer.parseInt
解析二进制字符串,然后将其转换为字节数组(使用ByteBuffer
),最后将字节数组转换为String
:
String bits = "01010011011011100110000101110010"
new String(
ByteBuffer.allocate(4).putInt(
Integer.parseInt(bits, 2)
).array(),
StandardCharsets.UTF_8
);
值 > 32 位
对于任意大的 bits
字符串,您也可以使用 BigInteger
:
new String(
new BigInteger(bits, 2).toByteArray(),
StandardCharsets.UTF_8
);
结果
Snar