如何从偶数/奇数条件中移除最后|
?
示例:
import java.io.*;
import java.net.Socket;
public class Test {
public static String _noManualTraceIt = "0008e07cd8ec6cfdb900283508004500004a7d8d400080060000c0a80087c0a800c6c6931388956304704eebd50c5018ffff82da0000674500000504000000000000030000000000000000000000060000000000ff210000";
public static void main(String[] args) throws IOException {
System.out.println( wireShirk(_noManualTraceIt) );
}
public static byte[] wireShirk(String bytes) throws IOException {
String temp = "";
String replaceIfOnLastFound = "||&|\r\n|\n\r|etc|etc";
int _i = 0;
do {
temp += bytes.charAt(_i);
if(_i%2 ==0) {
} else {
temp += "|";
}
System.out.println(bytes.length() + " " + _i + " " + two.charAt(_i) + " " + temp);
_i++;
} while(_i<bytes.length());
bytes = temp; // << replaceIfOnLastFound any of those characters
// | or & or \r\n or etc etc from that variable
String byteArrayStr[] = bytes.split("\\|");
byte bytesArray[] = new byte[byteArrayStr.length];
for (int i = 0; i < byteArrayStr.length; ++i) {
bytesArray[i] = (byte) (Integer.parseInt(byteArrayStr[i], 16));
}
return bytesArray;
}
}
输出:
176 175 0 00|08|e0|7c|d8|ec|6c|fd|b9|00|28|35|08|00|45|00|00|4a|7d|8d|40|00|80|06|00|00|c0|a8|00|87|c0|a8|00|c6|c6|93|13|88|95|63|04|70|4e|eb|d5|0c|50|18|ff|ff|82|da|00|00|67|45|00|00|05|04|00|00|00|00|00|00|03|00|00|00|00|00|00|00|00|00|00|00|06|00|00|00|00|00|ff|21|00|00|
答案 0 :(得分:3)
在循环中添加if语句..
} else {
if(i < bytes.length - 1){
temp += "|";
}
}
答案 1 :(得分:1)
重新排列添加管道符号和十六进制字符的步骤并添加条件
if (temp.length() > 0) {
temp += '|';
}
这样你首先添加分隔符,但不是第一次循环迭代。
答案 2 :(得分:0)
这是一个简单的例子,不使用任何逻辑,但是有一个常量(重新)分配的分隔符变量:
String separator = "";
String output = "";
for (String s : my_list) {
output += separator + s;
separator = "|";
}