2011-06-18 150 views
60

我有一個十六進制字符串(例如0CFE9E69271557822FE715A8B3E564BE),我想將它作爲字節寫入文件。例如,將字節寫入文件

Offset  0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
00000000 0C FE 9E 69 27 15 57 82 2F E7 15 A8 B3 E5 64 BE .þži'.W‚/ç.¨³åd¾ 

我該如何使用.NET和C#來完成此操作?

+1

可能http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa-in-c –

+1

的副本@斯蒂芬:只有部分。不是最重要的部分。 –

+1

[可以將字節\ [\]數組寫入C#中的文件的可能的重複?](http://stackoverflow.com/questions/381508/can-a-byte-array-be-written-to-a -file-in-c)(也可能只是部分重複)。 –

回答

113

如果我正確,這個你懂應該做的伎倆。如果您尚未擁有它,則需要在文件頂部添加using System.IO

public bool ByteArrayToFile(string fileName, byte[] byteArray) 
{ 
    try 
    { 
     using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) 
     { 
      fs.Write(byteArray, 0, byteArray.Length); 
      return true; 
     } 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine("Exception caught in process: {0}", ex); 
     return false; 
    } 
} 
63

最簡單的方法是將您的十六進制字符串轉換爲字節數組並使用File.WriteAllBytes方法。

this question使用StringToByteArray()方法,你會做這樣的事情:

string hexString = "0CFE9E69271557822FE715A8B3E564BE"; 

File.WriteAllBytes("output.dat", StringToByteArray(hexString)); 

StringToByteArray方法包含如下:

public static byte[] StringToByteArray(string hex) { 
    return Enumerable.Range(0, hex.Length) 
        .Where(x => x % 2 == 0) 
        .Select(x => Convert.ToByte(hex.Substring(x, 2), 16)) 
        .ToArray(); 
} 
+0

Thx,這工作正常。我怎樣才能將字節附加到同一個文件? (在第一個'字符串'之後) –

+1

@Robertico:你爲WriteAllBytes的第三個參數添加一個布爾值true。你有沒有發現MSDN?這是搜索WriteAllBytes附件時的第一個Google鏈接。 – 2011-06-18 20:43:00

+1

我收到一個錯誤,添加了布爾值'方法'WriteAllBytes'沒有重載'3'參數'。 MSDN介紹:'但是,如果您使用循環將數據添加到文件,BinaryWriter對象可以提供更好的性能,因爲您只需打開和關閉一次文件。「我正在使用循環。我使用@ 0A0D中的示例並將「FileMode.Create」更改爲「FileMode.Append」。 –

2

將十六進制字符串轉換爲字節數組。

public static byte[] StringToByteArray(string hex) { 
return Enumerable.Range(0, hex.Length) 
       .Where(x => x % 2 == 0) 
       .Select(x => Convert.ToByte(hex.Substring(x, 2), 16)) 
       .ToArray(); 
} 

信用:Jared Par

然後用WriteAllBytes寫入文件系統。

+0

如果你引用一個現有的Stack Overflow答案作爲這個問題的答案,那麼這是一個相當安全的選擇,這是一個重複的問題,應該標記爲這樣。 – ChrisF

+1

在這種情況下,它只回答了他的問題的一部分,所以我覺得它不需要被標記爲愚蠢。他只會在這方面得到一半的知識。 – Khepri

2
 private byte[] Hex2Bin(string hex) { 
     if ((hex == null) || (hex.Length < 1)) { 
      return new byte[0]; 
     } 
     int num = hex.Length/2; 
     byte[] buffer = new byte[num]; 
     num *= 2; 
     for (int i = 0; i < num; i++) { 
      int num3 = int.Parse(hex.Substring(i, 2), NumberStyles.HexNumber); 
      buffer[i/2] = (byte)num3; 
      i++; 
     } 
     return buffer; 
    } 

     private string Bin2Hex(byte[] binary) { 
     StringBuilder builder = new StringBuilder(); 
     foreach (byte num in binary) { 
      if (num > 15) { 
       builder.AppendFormat("{0:X}", num); 
      } else { 
       builder.AppendFormat("0{0:X}", num);/////// 大於 15 就多加個 0 
      } 
     } 
     return builder.ToString(); 
    } 
+0

Thx,這也很好。我怎樣才能將字節附加到同一個文件? (在第一個'字符串'之後) –