gpt4 book ai didi

Java字符串到位数组

转载 作者:行者123 更新时间:2023-11-30 08:15:16 24 4
gpt4 key购买 nike

考虑以下 String

String hexData = "1E01";

是否有一个简单实现将任何hexData转换成一个基于位的字符串数组,比如

String hexDataBits = "0001111000000001";

?

最佳答案

给你。使用内置的 parseInt 函数将十六进制字符串转换为 int 值,然后将其转换为二进制字符串。

public String hexToBinary(String hexBits) {
int intversion = Integer.parseInt(hexBits, 16);
String binaryVers = Integer.toBinaryString(intversion);
return binaryVers;
}

请注意,这不是填充的。如果你想填充它,修改binaryVers

例如:

// if you're dead set on having at least 16 chars, put this before the return statement
int padding = 16 - binaryVers.length();
while (padding > 0) {
binaryVers = "0" + binaryVers;
padding--;
}

关于Java字符串到位数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28950358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com