0

我如何|从我的偶数/奇数条件中删除最后一个?

例子:

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|
4

3 回答 3

2

在循环内添加 if 语句 ..

} else {
   if(i < bytes.length - 1){
    temp += "|";
   }
  }
于 2012-05-09T15:04:48.737 回答
1

重新排序添加管道符号和十六进制字符的步骤并添加条件

if (temp.length() > 0) {
  temp += '|';
}

这样,您首先添加分隔符,而不是第一次循环迭代。

于 2012-05-09T15:11:48.723 回答
0

这是一个简单的例子,没有使用任何逻辑,但是有一个分隔变量的常量(重新)分配:

String separator = "";
String output = "";
for (String s : my_list) {
  output += separator + s;
  separator = "|";
}
于 2012-05-09T15:08:07.820 回答