reading a text file

S

Stephen

I am reading a text file using TextReader reader = new
StreamReader("file.txt"); like I have done many a times before. I then
execute the statement string a = reader.ReadToEnd(); this string is then
sent into a function to convert its contents (hex string) into a byte array.
Well when this pass occurs the HextoByteArray function says the input is in
an invalid format. I think for somereason there is some whitespace/null
character at the end of the file that is getting read in. To debug I
manually sent the string into the function and it worked fine, so it is
somethign with my reading of the file.

Thanks
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Stephen said:
I am reading a text file using TextReader reader = new
StreamReader("file.txt"); like I have done many a times before. I then
execute the statement string a = reader.ReadToEnd(); this string is then
sent into a function to convert its contents (hex string) into a byte
array. Well when this pass occurs the HextoByteArray function says the
input is in an invalid format. I think for somereason there is some
whitespace/null character at the end of the file that is getting read in.
To debug I manually sent the string into the function and it worked fine,
so it is somethign with my reading of the file.

Can you post the HextoByteArray method?

Must probably you are trying to convert a string representation of a hex
stream into byte array.
 
S

Stephen

Here is the HexString to ByteArray method
private byte[] HexStringToByteArray(string hexStringIn)

{

char[] Delimiter = new char[1] { ' ' };

string[] HexNumbers = hexStringIn.Split(Delimiter);

byte[] NewByteArray = new byte[HexNumbers.Length];

for (int i = 0; i < HexNumbers.Length; i++)

{

NewByteArray =
byte.Parse(HexNumbers,System.Globalization.NumberStyles.AllowHexSpecifier);

}

return NewByteArray;

}

And I am attempting to pass it bf = HexStringToByteArray(s) where s is the
string that is read in using the streamreader mentioned in orginial message.
 
P

Peter Duniho

[...]
And I am attempting to pass it bf = HexStringToByteArray(s) where s is
the
string that is read in using the streamreader mentioned in orginial
message.

So what characters are in the string instance being passed to Parse() when
the exception occurs?

Where does the text file come from? Does it have multiple lines? Most
text files do, with each line separated by a line-break (CR/LF character
pair on Windows). You are only splitting on space characters, which means
that any line-breaks will be included in your strings to parse. Of
course, a line-break is not a valid character in a hex string.

Pete
 
G

Guest

Stephen
As Peter D. indicated, you might try something like this as the first line
in the body of your method:

private byte[] HexStringToByteArray(string hexStringIn)

{
hexStringIn = hexStringIn.Replace("\r","").Replace("\n","");

-- HTH,
Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
bogMetaFinder: http://www.blogmetafinder.com



Stephen said:
Here is the HexString to ByteArray method
private byte[] HexStringToByteArray(string hexStringIn)

{

char[] Delimiter = new char[1] { ' ' };

string[] HexNumbers = hexStringIn.Split(Delimiter);

byte[] NewByteArray = new byte[HexNumbers.Length];

for (int i = 0; i < HexNumbers.Length; i++)

{

NewByteArray =
byte.Parse(HexNumbers,System.Globalization.NumberStyles.AllowHexSpecifier);

}

return NewByteArray;

}

And I am attempting to pass it bf = HexStringToByteArray(s) where s is the
string that is read in using the streamreader mentioned in orginial message.

Stephen said:
Just for more information the error message image is included now.
 
S

Stephen

The text file is one that I created as an intermediate step in a larger
solution. It contains one line of a hexstring that is 50 characters long. I
have tried readline command and still no luck.

[...]
And I am attempting to pass it bf = HexStringToByteArray(s) where s is
the
string that is read in using the streamreader mentioned in orginial
message.

So what characters are in the string instance being passed to Parse() when
the exception occurs?

Where does the text file come from? Does it have multiple lines? Most
text files do, with each line separated by a line-break (CR/LF character
pair on Windows). You are only splitting on space characters, which means
that any line-breaks will be included in your strings to parse. Of
course, a line-break is not a valid character in a hex string.

Pete
 
P

Peter Duniho

The text file is one that I created as an intermediate step in a larger
solution. It contains one line of a hexstring that is 50 characters
long. I
have tried readline command and still no luck.

I wrote more than one question in my post. Answering all them will
provide information useful to addressing your question. You only answered
one of them.

If you don't answer all of the questions, you can't get all of the help
you might need.

In particular, the only question that you REALLY need to answer at this
point is the first one I asked:

"So what characters are in the string instance being passed to Parse()
when the exception occurs?"

Until you answer that one, no other question matters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top