File is read as binary (decimal?)
Okay, let me try to clear up this misunderstanding. In the programming world
we sometimes use words in ways that aren't always technically correct.
Computers deal in nothing but binary (in bytes). So if you think about it,
EVERY file is a "binary" file. However, when talking about data (files), we
use the term "binary" to refer to files that contain characters with no
printable representation. In general, this means that the file contains
bytes in the range of 0 -31, with the exception of a few bytes which control
printing, such as a carriage return (13), line feed (10), and tab (9). If a
file contains nothing but bytes from 32-255 (plus 9, 10, and/or 13) then we
refer to it as a "text" file, because you'll more than likely be able to
open it up in Notepad and see all of the characters in it without "weird"
stuff. The file you described in your original post would be considered a
"binary" file because it contains 2 and 0.
Now, as to binary, decimal, and hexadecimal. These are number systems (or,
apparently more correctly, "numeral systems":
http://en.wikipedia.org/wiki/Numeral_system). They are merely a
REPRESENTATION of a number made for the ease of human beings. It is possible
to represent (or display) the same amount of something in several different
ways. For example, consider the number of bars written below:
| | | | | | | | | | | |
In the decimal system, which is what humanity has basically adopted globally
(because we have 10 TOES, not fingers, or so anthropologists believe), this
amount is represented as "12".
In the binary system, this number is represented as "1100" (or, if you like
your binary numbers in 8-bit chunks, "00001100").
In the hexadecimal system, it's "C", or if you prefer 2 digits, "0C".
When switching between systems (changing "bases"), there is no conversion of
the amount. Twelve things are always twelve things, but there can be
conversions of the way the amount is DISPLAYED. This is what it means when
you "convert" decimal to hex, for example.
When you wrote this:
//Convert DEC (binary) byte[] to HEX
string content = null;
foreach (byte dec in data)
{
string content = += dec.ToString("X");
}
what you really meant was "make a 2-digit hexadecimal representation of the
values in the byte array and store it in a string." Saying that your bytes
are "in decimal" is incorrect. If anything, they are "in binary," but it
doesn't matter. The computer knows that twelve is twelve, and forty is
forty.
Back to the original issue: I don't know why you've turned your data into a
string of hex digits. I thought you were looking to extract "05012001" from
the string and turn it into the date 2001-05-01. (Which, by the way, is just
a way to DISPLAY a date, but that's another story. If you're American you
probably prefer 5/1/2001, and if you're European you probably want
1/5/2001.) You should do what I recommended: take the first 8 bytes and use
ASCIIEncoding.GetString() to get a string from them and then use
DateTime.Parse() to get a date.