This is a total noob question that I can't seem to find the answer to. I have a csv file that contains CAN frames. The frames look like this.
0.10836,1,181,2,8,6d,3d,05,00,28,00,54,12,1
Obviously this is hex. How can I read this into C# from the file as hex and not the ASCII equivalent?
I've tried using File.ReadAllBytes like this:
byte[] baFileLineArray = File.ReadAllBytes(sFileName);
And then a debug output like this:
foreach (byte ByteElement in baFileLineArray)
{
if (ByteElement == 0x2c)
{
Console.Write(Environment.NewLine);
}
else
{
Console.Write("{0:x} ", ByteElement);
}
}
Which produces the following output:
I need the information to be read as hex and not as an ASCII character. so for instance, the first from last field is 0x12, and the third from last is 0x54. Not 0x35 0x34.
Thanks So Much!!
I've tried using File.ReadAllBytes like this
That's the first mistake. This is a text file - so you should read it as a text file.
To be honest, if it's CSV you should probably look into CSV parsers which would make your life simpler, but you can do it yourself too. Something like:
foreach (string line in File.ReadLines(filename))
{
string[] part = line.Split(',');
// Deal with each part. Consider using Convert.ToInt32(part, 16)
// to parse a value as hex...
}
See more on this question at Stackoverflow