By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,786 Members | 1,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,786 IT Pros & Developers. It's quick & easy.

reading a text file

P: n/a
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
Jul 19 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi,

"Stephen" <no****@nospam.nowrote in message
news:uI**************@TK2MSFTNGP03.phx.gbl...
>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.
Jul 19 '07 #2

P: n/a
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[i] =
byte.Parse(HexNumbers[i],System.Globalization.NumberStyles.AllowHexSpecifi er);

}

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" <no****@nospam.nowrote in message
news:uO****************@TK2MSFTNGP05.phx.gbl...
Just for more information the error message image is included now.

"Stephen" <no****@nospam.nowrote in message
news:uI**************@TK2MSFTNGP03.phx.gbl...
>>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




Jul 19 '07 #3

P: n/a
On Thu, 19 Jul 2007 10:29:19 -0700, Stephen <no****@nospam.nowrote:
[...]
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
Jul 19 '07 #4

P: n/a
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" wrote:
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[i] =
byte.Parse(HexNumbers[i],System.Globalization.NumberStyles.AllowHexSpecifi er);

}

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" <no****@nospam.nowrote in message
news:uO****************@TK2MSFTNGP05.phx.gbl...
Just for more information the error message image is included now.

"Stephen" <no****@nospam.nowrote in message
news:uI**************@TK2MSFTNGP03.phx.gbl...
>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



Jul 19 '07 #5

P: n/a
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.

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Thu, 19 Jul 2007 10:29:19 -0700, Stephen <no****@nospam.nowrote:
[...]
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
Jul 19 '07 #6

P: n/a
On Thu, 19 Jul 2007 11:38:02 -0700, Stephen <no****@nospam.nowrote:
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.
Jul 19 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.