我已經嘗試了許多搜索來找到一種方法來搜索字符串的字節碼。這裏有一個例子:Java - 搜索字符串字節代碼
String stringThatHasBytes = "hello world hello world[[email protected]";
If stringThatHasBytes . Does have bytes {
return true or false
}
是否有可以搜索字節字符串的方法?
我已經嘗試了許多搜索來找到一種方法來搜索字符串的字節碼。這裏有一個例子:Java - 搜索字符串字節代碼
String stringThatHasBytes = "hello world hello world[[email protected]";
If stringThatHasBytes . Does have bytes {
return true or false
}
是否有可以搜索字節字符串的方法?
總之你不能這麼做。因爲每次打印時都會更改一個字節的打印。打印出字節不會打印出實際的字節,如果你正在尋找字節的精確比較,這是沒有意義的。
但是,如果您只查找字符串中的任何字節打印,只需控制字符串中的[[email protected]
s並返回true。
String stringThatHasBytes = "hello world hello world[[email protected]";
if (stringThatHasBytes.indexOf("[[email protected]") >= 0)
return true;
} else return false;
編輯:
如果你需要打印字節方式以有意義的方式,你應該轉換爲字節到一些有意義的文本,如:
public static String convertByteArrayToHexString(byte[] b) {
if (b != null) {
StringBuilder s = new StringBuilder(2 * b.length);
for (int i = 0; i < b.length; ++i) {
final String t = Integer.toHexString(b[i]);
final int l = t.length();
if (l > 2) {
s.append(t.substring(l - 2));
} else {
if (l == 1) {
s.append("0");
}
s.append(t);
}
}
return s.toString();
} else {
return "";
}
}
看看這個幫助
byte[] myByteArray = new byte[5];
myByteArray[0] = 'a';
myByteArray[1] = 'b';
myByteArray[2] = 'c';
myByteArray[3] = 'd';
myByteArray[4] = 'e';
for (byte x : myByteArray)
System.out.println(x);
String myString = "abcde";
System.out.println(myString.equals(new String(myByteArray)));
你是否想要將這些'char's轉換爲'byte's?因爲目前您有冗餘轉換和類型不一致。 – ApproachingDarknessFish
感謝提醒 – Hitman47
會包含太簡單的a爲你服務嗎?
boolean hasBytes = str.contains("[[email protected]");
如果是這樣,讓我知道我會告訴你一些很好的正則表達式!但這應該足夠了。
如何獲取整個字節部分? – user1978786
我不確定你在問什麼。你在尋找一個正則表達式來匹配默認的Java Object toString輸出嗎?你需要匹配*任何*類型的快捷鍵,或只是[B @ [0-9a-f] {6}或...? –
由於''[B @ 9304b1「'看起來像字節數組上的'toString()'的輸出,因此它看起來像是用一個'byte []'連接了一個字符串。爲什麼?你在那個字符串中尋找什麼「字節碼」? –
如果我理解正確,你需要定義一個模式,將匹配這個字符串與_byte code_在它。所以,最有可能的是使用'regepx'和[String#matches](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#matches(java。 lang.String))方法。 – zoom