2010-05-20 66 views
1

字節數組當我有這樣一個字符串「0xd8 0xFF的0xe0的」我做位對流:從字符串

Text.Split(' ').Select(part => byte.Parse(part, System.Globalization.NumberStyles.HexNumber)).ToArray(); 

但是,如果我得到的字符串,如「0xd8ffe0」我不知道該怎麼辦?

另外我能夠推薦如何將字節數組寫成一個字符串。

回答

2

您需要在開始分析之前先擦洗你的字符串。首先,刪除前面的0x,然後在枚舉字符串時跳過任何空格。但使用LINQ可能不是最好的方法。首先,代碼的可讀性不好,如果您正在調試,則很難一步步完成。但是,也有一些技巧可以使非常快速地進行十六進制/字節轉換。例如,請勿使用Byte.Parse,而應使用數組索引來查找相應的值。

後來我實現了一個HexEncoding類,它從Encoding基類派生得很像ASCIIEncoding和UTF8Encoding等等。使用它非常簡單。它也非常優化,根據數據的大小,這可能非常重要。

var enc = new HexEncoding(); 
byte[] bytes = enc.GetBytes(str); // convert hex string to byte[] 
str = enc.GetString(bytes);  // convert byte[] to hex string 

這是完整的類,我知道它對於帖子有點大,但是我已經刪除了文檔註釋。

public sealed class HexEncoding : Encoding 
{ 

    public static readonly HexEncoding Hex = new HexEncoding(); 

    private static readonly char[] HexAlphabet; 
    private static readonly byte[] HexValues; 

    static HexEncoding() 
    { 

     HexAlphabet = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 

     HexValues = new byte[255]; 
     for (int i = 0 ; i < HexValues.Length ; i++) { 
      char c = (char)i; 
      if ("abcdefABCDEF".IndexOf(c) > -1) { 
       HexValues[i] = System.Convert.ToByte(c.ToString(), 16); 
      } // if 
     } // for 

    } 

    public override string EncodingName 
    { 
     get 
     { 
      return "Hex"; 
     } 
    } 

    public override bool IsSingleByte 
    { 
     get 
     { 
      return true; 
     } 
    } 

    public override int GetByteCount(char[] chars, int index, int count) 
    { 
     return count/2; 
    } 

    public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) 
    { 

     int ci = charIndex; 
     int bi = byteIndex; 

     while (ci < (charIndex + charCount)) { 

      char c1 = chars[ci++]; 
      char c2 = chars[ci++]; 

      byte b1 = HexValues[(int)c1]; 
      byte b2 = HexValues[(int)c2]; 

      bytes[bi++] = (byte)(b1 << 4 | b2); 

     } // while 

     return charCount/2; 

    } 

    public override int GetCharCount(byte[] bytes, int index, int count) 
    { 
     return count * 2; 
    } 

    public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) 
    { 

     int ci = charIndex; 
     int bi = byteIndex; 

     while (bi < (byteIndex + byteCount)) { 

      int b1 = bytes[bi] >> 4; 
      int b2 = bytes[bi++] & 0xF; 

      char c1 = HexAlphabet[b1]; 
      char c2 = HexAlphabet[b2]; 

      chars[ci++] = c1; 
      chars[ci++] = c2; 

     } // while 

     return byteCount * 2; 

    } 

    public override int GetMaxByteCount(int charCount) 
    { 
     return charCount/2; 
    } 

    public override int GetMaxCharCount(int byteCount) 
    { 
     return byteCount * 2; 
    } 

} // class 
+0

不錯,我在一個項目中實現了一種編碼,但是我沒有訪問代碼的權限。你能發佈完整的代碼(或鏈接)嗎?版權? – brickner 2010-05-20 06:11:19

+0

這是完整的代碼。我自己寫了,但我現在把它放在公有領域。 :) – Josh 2010-05-20 06:13:13

1
  1. 六角Stringbyte[]

    byte[] bytes = new byte[value.Length/2]; 
    for (int i = 0; i < value.Length; i += 2) 
    { 
        bytes[i/2] = Convert.ToByte(value.Substring(i, 2), 16); 
    } 
    

    如果你在一開始有"0x"你應該跳過兩個字節。

  2. byte[]或任何IEnumerable<Byte> - >六角String

    return sequence.Aggregate(string.Empty, 
              (result, value) => result + 
                   string.Format(CultureInfo.InvariantCulture, "{0:x2}", value));