2013-04-29 10 views
5

मैं एक बाइट सरणी है कि कुछ इस तरह दिखता है के रूप में तत्वों की एक अनुक्रम का उपयोग खोज और पार्स करने के लिए कैसे एक सरणी:ठीक से लक्ष्य

byte[] exampleArray = new byte[] 
         { 0x01, 0x13, 0x10, 0xe2, 0xb9, 0x13, 0x10, 0x75, 0x3a, 0x13 }; 

मेरे अंतिम लक्ष्य उप सरणी के किसी भी समय मैं में इस सरणी विभाजित करने के लिए है अनुक्रम { 0x13, 0x10 } देखें। इसलिए उदाहरण सरणी पर मेरे वांछित परिणाम होगा:

{ 0x01 } 
{ 0xe2, 0xb9 } 
{ 0x75, 0x3a, 0x13 } 

आदर्श रूप में, मैं यह भी पता चला है कि अंतिम सरणी, { 0x75, 0x3a, 0x13 }, खोज अनुक्रम के साथ खत्म नहीं हुई तो यह है कि मैं एक विशेष रूप में है कि के साथ काम कर सकता था की आवश्यकता होगी मामला।

सर्वोत्तम दृष्टिकोण पर कोई विचार?

+0

क्या होगा यदि आप अपने सरणी ot ascii स्ट्रिंग को परिवर्तित करते हैं और string.split का उपयोग करते हैं? कुछ 'Encoding.ASCII.GetString (exampleArray) की तरह। .Split (...) ' – Vladimir

+0

इसे देखें: http://stackoverflow.com/a/4617264/848330 – nmat

+1

' स्ट्रिंग delimiter = Encoding.ASCII.GetString (नया बाइट [ ] {0x13, 0x10}); ' – Vladimir

उत्तर

1
List<byte[]> Split(byte[] bArray) 
     { 
      List<byte[]> result = new List<byte[]>(); 
      List<byte> tmp = new List<byte>(); 
      int i, n = bArray.Length; 
      for (i = 0; i < n; i++) 
      { 
       if (bArray[i] == 0x13 && (i + 1 < n && bArray[i + 1] == 0x10)) 
       { 
        result.Add(tmp.ToArray()); 
        tmp.Clear(); 
        i++; 
       } 
       else 
        tmp.Add(bArray[i]); 
      } 
      if (tmp.Count > 0) 
       result.Add(tmp.ToArray()); 
      return result; 
     } 

पिछले सरणी अनुक्रम के साथ समाप्त नहीं कर सकते हैं, किसी भी splited हिस्सा विभाजक से contein नहीं है। केवल बाइट 0x13 हो सकता है, इसलिए यदि यह आपके लिए आयातक है, तो आप अंतिम उप सरणी के अंतिम बाइट को चेक कर सकते हैं।

+0

इससे सूचकांक सीमाओं से बाहर हो जाएगा; आपको यह सुनिश्चित करने की ज़रूरत है कि यदि आप शरीर के भीतर "अगली" वस्तु को चेक करते हैं तो आपका 'लूप' अंत तक सभी तरह से नहीं जाता है। – Servy

+0

मैंने इस जवाब को स्वीकार कर लिया क्योंकि यह मेरे द्वारा किए गए कार्यों के सबसे नज़दीक है। धन्यवाद। –

1

कैसे कुछ इस तरह है कि (बेहतर त्रुटि जाँच के साथ स्पष्ट रूप से!) सामान्य स्थिति में काम करना चाहिए के बारे में:

using System; 
using System.Collections.Generic; 
using System.Linq; 

class Program 
{ 
    static void Main(string[] args) 
    { 
     byte[] exampleArray = new byte[] { 1, 2, 3, 4, 5, 2, 3, 6, 7, 8 }; 

     var test = exampleArray.PartitionBySubset(new byte[] { 2, 3 }).ToList(); 
    } 
} 

public static class Extensions 
{ 
    public static IEnumerable<IEnumerable<T>> PartitionBySubset<T>(this IEnumerable<T> sequence, IEnumerable<T> subset) where T : IEquatable<T> 
    { 
     // Get our subset into a array so we can refer to items by index and so we're not iterating it multiple times. 
     var SubsetArray = subset.ToArray(); 
     // The position of the subset array we will check 
     var SubsetArrayPos = 0; 
     // A list to hold current items in the subsequence, ready to be included in the resulting sequence 
     var CurrentList = new List<T>(); 

     foreach (var item in sequence) 
     { 
      // add all items (ones part of the subset will be removed) 
      CurrentList.Add(item); 
      if (item.Equals(SubsetArray[SubsetArrayPos])) 
      { 
       // This item is part of the subset array, so move to the next subset position 
       SubsetArrayPos++; 
       // Check whether we've checked all subset positions 
       if (SubsetArrayPos == SubsetArray.Length) 
       { 
        // If so, then remove the subset items from the current list 
        CurrentList.RemoveRange(CurrentList.Count - SubsetArray.Length, SubsetArray.Length); 
        // Reset the position 
        SubsetArrayPos = 0; 

        // Only return the list if it's not empty (the main list could start with a subset) 
        if (CurrentList.Count != 0) 
        { 
         // Return the list we have now since it's been ended. 
         yield return CurrentList; 
         // Create a new list ready for more items 
         CurrentList = new List<T>(); 
        } 
       } 
      } 
      else 
      { 
       // This item isn't part of the subset, so next time check the start. 
       SubsetArrayPos = 0; 
      } 
     } 

     // If we get to the end and have some items to return, then return them. 
     if (CurrentList.Count != 0) 
      yield return CurrentList; 
    } 
} 
0
string example = Encoding.ASCII.GetString(exampleArray); 
string delimiter = Encoding.ASCII.GetString(new byte[] { 0x13, 0x10 }); 
string[] result = example.Split(new string[] { delimiter}); 
string ending = Encoding.ASCII.GetString(new byte[] { 0x75, 0x3a, 0x13 }); 
bool ends = example.EndsWith(ending); 
0

मैं वर्तमान और पिछले सरणी तत्व की जांच के साथ आगे बढ़ूंगा। इस तरह:

int[] data = ...; 

// start from second byte, to be able to touch the previous one 
int i = 1; 
while (i < data.Length) 
{ 
    if (data[i-1] == 0x13 && data[i] == 0x10) 
    { 
     // end of subarray reached 
     Console.WriteLine(); 
     i+=2; 
    } 
    else 
    { 
     // output the previous character, won't be used in the next iteration 
     Console.Write(data[i-1].ToString("X2")); 
     i++; 
    } 
} 

// process the last (or the only) byte of the array, if left 
if (i == data.Length) 
{ 
    // apparently there wasn't a delimiter in the array's last two bytes 
    Console.Write(data[i-1].ToString("X2")); 
    Console.WriteLine(" - no 0x13 010"); 
} 

नोट: प्रदर्शन के लिए कंसोल आउटपुट। वास्तविक डेटा प्रोसेसिंग के साथ बदलें।

संबंधित मुद्दे