2012-12-18 159 views
0

我有一個編碼功能,創造了我的命令,我 假設我有在字節字符串像(0010010210004443331012011101000)數據編碼和解碼

輸出I確實需要創建一個DECODE函數提取一些數據從該字節允許用戶從8位到12,並提取後,這個檢查說,如果陣列的第一個字節是00或者沒有,那麼返回我在ASCII

這裏提取數據是我的解碼

這裏是我的解碼是完全錯誤

public byte[] Decode(string Resp) 
{ 

    string NewResp = Resp; 
     string SubResp = NewResp.Substring(65, 185); 
     Console.WriteLine("Substring: {0}", SubResp); 

     MessageBox.Show(SubResp); 
    return null; 
} 

這裏是編碼

{ 


     return bCommand; 
    } 
+0

好,做ü有編碼算法中? –

+0

是的,我確實有編碼我加入那裏 –

+0

爲什麼NewResp.Substring(65,185); < - 我沒有得到這部分? –

回答

0
class Program 
    { 
     static void Main(string[] args) 
     { 
      byte[] encoded = 
       { 
        0x01, 0x00, 0x00, 0x24, 0x02, 0x43, 0x31, 0x31, 0x00, 0x00, 0x01, 0x43, 0x49, 0x53, 0x2D, 
        0x34, 0x36, 0x58, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x11 
       }; 

      byte[] toCheck = SubArray<byte>(encoded, 8, 5); // start index at 8 and u need till 12th 

      if (toCheck[0] != '\0') 
      { 
       Console.WriteLine(Encoding.ASCII.GetString(toCheck)); 
      } 

      Console.Read(); 
     } 

     public static T[] SubArray<T>(T[] data, int index, int length) 
     { 
      T[] result = new T[length]; 
      Array.Copy(data, index, result, 0, length); 
      return result; 
     } 

    } 
+0

我如何區分這?該編碼和解碼應該是模塊化的,並且應該從外部調用函數來構建 –

0

你有十六進制串像

string hexstr= "68696d616e736875"; 
string subHexstr= hexstr.Substring(2,3); // Lets say index from 3rd byte to 5th byte. 
byte[] by=new byte[subHexstr.Length]; 
     int j = 0; 
     for (int i = 0; i < by.Length; i++) 
     { 
      by[i] = byte.Parse(subHexstr.Substring(j, 2), System.Globalization.NumberStyles.HexNumber); 
      j = j + 2; 
     } 

if(by[0]!=0x00) 
{ 
    string asciiStr= Encoding.Ascii.GetString(by); 
}