What is my mistake as I'm unable to find an example on the Internet that matches what I'm doing or at least I'm not sure if it does?
The problem I'm having is it does not like
hexIn = fileStream.Read()
Code:
FileStream fileStream = new FileStream(fileDirectory, FileMode.Open, FileAccess.Read);
String s;
try
{
for (int i = 0; (hexIn = fileStream.Read() != -1; i++)
{
s = hexIn.ToString("X2");
//The rest of the code
}
}
finally
{
fileStream.Close();
}
Missing ")". . Try:
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
s=...
}
}
There are a few things I would do differently.
The first, is you should using FileStream
with a using
. But actually, if you're just trying to read the lines in a text file, a StreamReader
would be ok:
try
{
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
// convert line to Hex and then format with .ToString("X2")
}
}
}
catch
{
// handle error
}
If you're trying to convert your entire input file to a hex value, let us know. I'll just assume line-by-line for now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With