2015-04-15 57 views
0

我想從下面的字節數組消息中獲取STX(0x02)的位置。如果你看到這個消息,它在很多地方都有0x2,但是我想要的唯一位置就是STX。我一直使用for循環向後循環。我必須順便循環回來。我嘗試了很多方法,但我很難獲得這個位置。我試過但沒有奏效的一種方法是,無論哪裏出現0x2,並且在它和前面的ETX(0x3)之間具有3個或更多元素,就可以獲得該STX的那個位置。但是我做錯了什麼,因爲我一直在得到一個我很難解決的錯誤。你能幫忙嗎?Java使用數組列表獲取值的位置

編輯:如果有更好的方法,那麼我可以通過將它與另一個0x2區分開來找到那個位置(STX)的邏輯,請你提供一下。

編輯:我必須向後循環,因爲這是給我的指示所要求的。

編輯:這裏是代碼:

//Test 3: 
    public String Test3(List<Byte> byteList) { 
     //checking positions of the STX and ETX 
     //looping through the array backwards 
     for (int i = byteList.size() - 1; i >= 0; i--) {     
      if (byteList.get(i) == STX && (i >= 3 && byteList.get(i) == ETX)) { 
       STXpos = i; 
      } 
     }  
     return STXpos; 
    } 
    byte[] validMsgWithRandomData = {0x32,0x32,0x32, //Random data 
    0x02, // STX 
    0x31,0x32,0x10,0x02,0x33, // Data 31 32 02 33 
    0x03, // ETX 
    0x31^0x32^0x02^0x33^0x03,// LRC calculated from the data (with the DLE removed) plus the ETX 
    0x2,0x3,0x00,0x02 //Random data 
}; 
+0

請分享您使用的代碼循環訪問數組。 – CubeJockey

+0

「我必須順帶向後循環。」 - >爲什麼?至於核心問題......有一些標準算法用於查找文本中的模式,如[KMP](http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%% 93Pratt_algorithm)和[BM](http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm) – Turing85

+0

byteList.get(i)== STX'既可以是true也可以是byteList。得到(i)== ETX'除非'STX == ETX'?你在STX測試中錯過了「我 - 某事」嗎? –

回答

1

由於STX和ETX之間的元素量不恆定的,我會在正常的順序來搜索和尋找ETX我發現後,STX:

public String Test3(List<Byte> byteList) { 
    // find potential STX 
    for (int i = 0; i < byteList.size(); ++i) { 
     if (byteList.get(i) == STX) { 
      // make sure matching ETX exists 
      for (int j = i + 1; j < byteList.size(); ++j) { 
       if (byteList.get(j) == ETX) { 
        return i; 
       } 
      } 
     } 
    } 
} 

你也可以做相反的順序,如果你真的想:

public String Test3(List<Byte> byteList) { 
    // find potential ETX 
    for (int i = byteList.size() - 1; i > 0; --i) { 
     if (byteList.get(i) == ETX) { 
      // make sure matching STX exists 
      for (int j = i - 1; j > 0; --j) { 
       if (byteList.get(j) == STX) { 
        return j; 
       } 
      } 
     } 
    } 
} 

順便說一下,如果你想要強制 STX和ETX之間的元素距離,你可以通過改變j的初始化來實現。

+0

謝謝你,這是完美的。我現在可以解決這個問題,並嘗試向後循環,並看看我得到了什麼。 – Kaal

+0

np。你爲什麼想要向後循環? –

+0

,因爲我一直在做其他消息字節數組的其他測試,其中一個消息有兩條消息(第一條消息是舊消息,後面是新消息)。最近一次使用STX是有效的,這是第二條消息,所以向後循環有意義找到第一個stx位置,並且它完美地工作。但這是一個完全不同的問題。 – Kaal

2

我第一次嘗試後向循環和O(n)複雜性。

編輯:擺脫STX的候選人。編輯2:此解決方案至少適用於OP的一個案例(但尚未廣泛測試......)。

final int NOTFOUND = -1; 
    final int ETX = 0x03; 
    final int STX = 0x02; 
    int stxpos = NOTFOUND; 
    int etxpos = NOTFOUND; 
    int etxcandidatepos = NOTFOUND; 
    for (int i = validMsgWithRandomData.length - 1; i >=0; --i) 
    { 
     if (ETX == validMsgWithRandomData[i]) 
     { 
      etxcandidatepos = i; 
      if (NOTFOUND == etxpos) 
      { 
       etxpos = i; 
       stxpos = NOTFOUND; 
      } 
     } 
     else if (STX == validMsgWithRandomData[i]) 
     { 
      if (NOTFOUND != etxpos) 
      { 
       stxpos = i; 
       if (NOTFOUND != etxcandidatepos) 
       { 
        etxpos = etxcandidatepos; 
        etxcandidatepos = NOTFOUND; 
       } 
      } 
     } 
    } 
+0

謝謝你,你的工作做得倒退了。 – Kaal