how would i go about taking characters from a text and storing them in a byte array? by hex characters i mean the text file reads "00 9A 6F AE" etc. etc.
does system.io.streamreader only store data as strings?
and id like to store it in a byte[], instead of manually typing
{ 0x00, 0x9a, 0x6f, 0xae } into the code itself.
thanks,
mythos
Loading
AlanPosted Jul 8, 2008, 2:06 PM
Try this (.NET 2.0 or later):
using System;
using System.IO;
using System.Collections.Generic;
class Program list = new List();
{
static void Main()
{
string[] lines = File.ReadAllLines("myfile.txt"); // say
List
foreach(string line in lines)
{
string[] hexes = line.Split(' ');
foreach (string hex in hexes)
{
list.Add(Convert.ToByte(hex, 16));
}
}
byte[] bytes = list.ToArray();
// check it worked
Console.Write("{ ");
for(int i = 0; i < bytes.Length; i++)
{
Console.Write("0x" + bytes[i].ToString("x2"));
if (i < bytes.Length - 1) Console.Write(",");
}
Console.WriteLine(" }");
Console.ReadKey();
}
}