Java 为字符串数组指定一个二进制值
例如,我试图给一个字符串数组一个二进制值Java 为字符串数组指定一个二进制值,java,Java,例如,我试图给一个字符串数组一个二进制值 String list_command[]= {"movie", "audio", "games", "current_time", "city", "city"}; 大概是 list_command = 000,001,010,011,100,101 您可以尝试使用词典。我不确定您想要的键/值是什么,但是如果您想通过list\u command[]中的字符串进行查找,您可以尝试以下方法: Dictionary<string, string&
String list_command[]= {"movie", "audio", "games", "current_time", "city", "city"};
大概是
list_command = 000,001,010,011,100,101
您可以尝试使用
词典
。我不确定您想要的键/值是什么,但是如果您想通过list\u command[]
中的字符串进行查找,您可以尝试以下方法:
Dictionary<string, string> commandsToBinary = new Dictionary<string,string>();
for(int i = 0; i < list_command.Length; i++){
string binaryCommand = Convert.ToString(i,2); // Get the binary representation of `i` as a string.
commandsToBinary.Add(command, binaryCommand); //switch command & binaryCommand to have the binary strings as your keys.
}
Dictionary命令stobinary=newdictionary();
对于(int i=0;i
构建二进制数组
public static byte[][] toBinary(String... strs) {
byte[][] value = new byte[strs.length][];
for(int i=0; i<strs.length; i++) {
value[i] = strs[i].getBytes();
}
return value;
}
你想要有人来编码吗?你只是想要二进制的数组索引,还是想把值转换成二进制值?没有所谓的“二进制值”。如果你有一个整数值,你可以把它写成二进制数,或十进制数,依此类推。您的示例并没有给出“数组a(二进制)值”,而是将字符串与您选择(出于任何原因)表示为二进制数的值相关联。
public static String[] toStrings(byte[][] bytes) {
String[] value = new String[bytes.length];
for(int i=0; i<bytes.length; i++) {
value[i] = new String(bytes[i]);
}
return value;
}
public static void print(byte[][] bytes) {
for(byte[] bArray : bytes) {
StringBuilder binary = new StringBuilder();
for (byte b : bArray)
{
int val = b;
for (int i = 0; i < 8; i++)
{
binary.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
binary.append(' ');
}
System.out.println(binary.toString());
}
}
public static void main(String... args) {
print(toBinary("Hello", "World"));
}