0

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!!

1
  • In what way is 0.10836 hex?
    – Jon Skeet
    Jan 25, 2014 at 18:00

2 Answers 2

1

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...
}
-1

using a variable of stringstream. and get one line of file information in string buffer and convert to hex with this syntax stramstringvari<

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.