如何将String解析为Binary并将其转换为Java中等效的UTF-8? [英] How to parse String as Binary and convert it to UTF-8 equivalent in Java?
问题描述
我需要将String内容解析为二进制序列,并将其转换为等效于 UTF-8 的String.
I need to parse String content as binary sequence and convert them to its UTF-8 equivalent String.
例如, B , A 和 R 的 UTF-8 二进制等效项如下:
B = 01000010
A = 01000001
R = 01010010
For example, UTF-8 binary equivalents of B, A and R are as follows:
B = 01000010
A = 01000001
R = 01010010
现在,我需要将字符串"010000100100000101010010" 转换为字符串"BAR"
即对于上述情况,将具有24个字符的输入字符串分成三个相等的部分(每部分8个字符),并转换为与它等效的 UTF-8 字符串值.
Now, I need to convert a string "010000100100000101010010" to string "BAR"
i.e. For above case input string with 24 characters are divided into three equal parts(8 character in each part) and translated to its UTF-8 equivalent as a String value.
示例代码:
public static void main(String args[]) {
String B = "01000010";
String A = "01000001";
String R = "01010010";
String BAR = "010000100100000101010010";
String utfEquiv = toUTF8(BAR);//expecting to get "BAR"
System.out.println(utfEquiv);
}
private static String toUTF8(String str) {
// TODO
return "";
}
方法 toUTF8(String str){}
推荐答案
您应该将其分为两个问题:
You should separate this into two problems:
- 通过解析二进制值将字符串转换为字节数组
- 使用UTF-8将字节数组转换回字符串
使用new String(bytes, StandardCharsets.UTF_8)
,后者非常简单.
对于第一部分,棘手的部分是Byte.parseByte
不会自动处理前导1 ...因此,我可能会将每个8位字符串解析为short
,然后转换为byte
:
For the first part, the tricky part is that Byte.parseByte
won't automatically handle a leading 1... so I'd probably parse each 8-bit string into a short
and then cast to byte
:
public static byte[] binaryToBytes(String input) {
// TODO: Argument validation (nullity, length)
byte[] ret = new byte[input.length() / 8];
for (int i = 0; i < ret.length; i++) {
String chunk = input.substring(i * 8, i * 8 + 8);
ret[i] = (byte) Short.parseShort(chunk, 2);
}
return ret;
}
这篇关于如何将String解析为Binary并将其转换为Java中等效的UTF-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!