I'm trying to convert a string of bits into Unicode characters in java. Problem is that I only get chines signs etc.
String bits = "01010011011011100110000101110010"
Anyone know how to do this?
Use Integer.parseInt
to parse the binary string, then convert it to byte array (using ByteBuffer
) and finally convert byte array to String
:
String bits = "01010011011011100110000101110010"
new String(
ByteBuffer.allocate(4).putInt(
Integer.parseInt(bits, 2)
).array(),
StandardCharsets.UTF_8
);
For arbitrary large bits
String you can use also BigInteger
:
new String(
new BigInteger(bits, 2).toByteArray(),
StandardCharsets.UTF_8
);
Snar
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.