0
我想給一個字符串數組的二進制值例如。給出一個字符串數組的二進制值
String list_command[]= {"movie", "audio", "games", "current_time", "city", "city"};
會像
list_command = 000,001,010,011,100,101
我想給一個字符串數組的二進制值例如。給出一個字符串數組的二進制值
String list_command[]= {"movie", "audio", "games", "current_time", "city", "city"};
會像
list_command = 000,001,010,011,100,101
創建二進制數組
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;
}
生成字符串數組
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;
}
打印值:源(Convert A String (like testing123) To Binary In Java)
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"));
}
你可以嘗試使用Dictionary
。我不知道你想什麼,你的鑰匙/值,但如果你想查找由您在list_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.
}
? – Kick
你只是想要二進制數組索引,或者你是否想將值轉換爲二進制值? – AntonH
沒有「二元價值」這樣的東西。如果你有一個整數值,你可以把它寫成一個二進制*數字*,或一個小數*數字*,等等。而你的例子並沒有給出「一個數組(一個二進制)值」,它將字符串與你選擇表示的數值(無論出於何種原因)相關聯爲二進制數。 – laune