2017-07-28 176 views
0

我有一個字符串數組轉換字符串數組轉換爲十六進制陣列

Receivedbyte[0]=5A 
Receivedbyte[1]=3A 
Receivedbyte[2]=7A 
Receivedbyte[3]=60 

我想將它們視爲十六進制數和0x20的異或的每個值。 所以我想我的數據是 0x5A^0x20在第0個位置。等等。

我嘗試了以下操作,但出現錯誤說輸入字符串格式不正確。

static public string[] escapeFix(string[] Receivedbyte) 
     {   
      uint temp = Convert.ToUInt32(Receivedbyte[1]); 

      temp = temp^0x20; 
      Receivedbyte[0] = Convert.ToString(temp); 
      Receivedbyte[1] = Receivedbyte[2]; 
      Receivedbyte[2] = Receivedbyte[3]; 
      return Receivedbyte; 
     } 
+0

您定位的語言是? C#? –

回答

0

Convert.ToUInt32試圖解析十進制字符串,但你輸入十六進制,因此錯誤。嘗試byte.Parse(ReceivedBytes[1], NumberStyles.AllowHexSpecifier)

uint.ToString()也轉換爲十進制表示。你的意思是轉換爲十六進制?那麼你應該.ToString("X")

您的代碼在解析完成後會執行什麼操作,與您所描述的內容完全相反。 您將以[「26」,「7A」,「60」,「60」]結尾,其中「26」是0x3A^0x20,26的十進制表示。

爲什麼你首先搞亂了字符串?你不能只使用byte[]?像:

public static byte[] EscapeFix(byte[] receivedBytes) 
{ 
    return receivedBytes.Select(b => (byte)(b^0x20)).ToArray(); 
} 
相關問題