reading ascii strings

H

Hector

I am trying to read some ascii strings from a file and adding each strings
to a listbox control. But the list box does it correctly. Any suggestions..

Here are some of the strings

"\n01\00512\0A0\r"
"\n01\0OKAY\0@\r"

But it only displays []01
 
M

Miha Markic

Hi Hector,

I guess combination of \ and char is taken as a special symbol (\n is
newline, \0 it probably the line terminator).
How exactly are you reading them and how are you adding them to listbox?
Doubling backslash \\ instead of \ will certainly cure the problem.
 
K

Ken Pinard

Have you checked the source for these entries? If you wrote these lines out
with a write line statement the \0 would terminate the line write to the
file (rest of line lost). the \n would write a chr(13) new line, out to the
file. I would check to see how you are getting this data. If you need the
\'s in the strings then double slash them on the writes.

HTH

Ken

Miha Markic said:
Hi Hector,

I guess combination of \ and char is taken as a special symbol (\n is
newline, \0 it probably the line terminator).
How exactly are you reading them and how are you adding them to listbox?
Doubling backslash \\ instead of \ will certainly cure the problem.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com
www.rhand.com

Hector said:
I am trying to read some ascii strings from a file and adding each strings
to a listbox control. But the list box does it correctly. Any suggestions..

Here are some of the strings

"\n01\00512\0A0\r"
"\n01\0OKAY\0@\r"

But it only displays []01
 
N

Nick Malik

Hi Hector,
as others have pointed out, \ followed by a character is an indicator to the
system that you wanted a non-printable character.

\0 is a null character

the listbox control will stop at a null character. This behavior is
standard in Windows, because the C++ language terminates strings with a null
character.

also in your string:
\n is a newline
\r is a carriage return

If double-slash makes your code unreadable, consider this notation:

string myString = @"\n01\00512\0A0\r";

this is exactly the same as:

string myString = "\\n01\\00512\\0A0\\r";

I hope this helps.
--- Nick
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hector,

Ensure you've specified the correct encoding for the FileStream instance you
use to read from the ASCII file. The encoding is Unicode by default.
 

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