|
-
Apr 10th, 2004, 04:03 PM
#1
Can't get this code to display £ chars
I have a simple text file created with notepad and I am trying to load it into a textbox with this code:
VB Code:
If ofd.ShowDialog = DialogResult.OK Then
If File.Exists(ofd.FileName) Then
Dim sr As StreamReader = New StreamReader(ofd.FileName)
Try
txtOriginal.Text = sr.ReadToEnd
Catch ex As Exception
MessageBox.Show("Could not load that file: " & ex.Message, "Censor", MessageBoxButtons.OK)
Finally
'tidy up a bit
txtOriginal.SelectionLength = 0
sr.Close()
End Try
End If
End If
My file has some "£" (British Pounds Sterling symbol, (Shift + 3) on most keyboards.
The whole file loads ok except for the sterling signs. What gives? If I go out of my way to make it work, save the file in unicode, and try again it works OK, but not all of my files will be in unicode).
I'm pretty sure it should show EVERY single printable char in the file (regardless of its encoding), in the textbox, yet this is not the case.
Last edited by wossname; Apr 10th, 2004 at 04:07 PM.
I don't live here any more.
-
Apr 10th, 2004, 07:24 PM
#2
Shift + 3 = # on my keyboard
Actually I don't think I have £ on my keyboard at all. The closest thing I have is &. I can probably find it by holding ctrl or alt and some numbers but I'm not sure.
Anyway you can apply different encoding to the streamreader when you use it. This worked for me:
Dim sr As StreamReader = New StreamReader(ofd.FileName, System.Text.Encoding.UTF7)
I just changed that line in your code by adding the System.Text.Encoding.UTF7 and it should the £ after that.
Last edited by Edneeis; Apr 10th, 2004 at 07:29 PM.
-
Apr 10th, 2004, 07:35 PM
#3
I wonder how many charact
hehe... I tried everything except UTF7... figuring UTF8 must be just a more-feature rich encoding...
-
Apr 11th, 2004, 11:57 AM
#4
So how do I know at runtime what the encoding will be? it could be ANY kind of file imaginable.
I don't live here any more.
-
Apr 11th, 2004, 02:37 PM
#5
I don't know much about Encoding but I don't see why you'd need to know what encoding was used for the file. To read the character you want I think you'll need to Read it using the UTF7 encoding regardless of what it was written in. I can still read files in other encodings it just changes how certain characters are interpeted. Try it and see what happens.
-
May 23rd, 2005, 04:30 AM
#6
Frenzied Member
Re: Can't get this code to display £ chars
I have tried to open several files in this way. The text for some characters is non-displayable, and the ASC code for them all is 63 ("?") though the characters are not question marks and are not the same character.
It is causing me a problem because these characters in the file are part of the delimiter that separates data. I cannot split the text over the delimiter because VB.Net doesn't compare it correctly, whether I use the Mid() to compare a portion of the text string itself, or type in chr(63) for these characters as the splitter. It won't work.
Any thoughts?
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
May 23rd, 2005, 04:35 AM
#7
Frenzied Member
Re: Can't get this code to display £ chars
also, if I paste the delimiter string into the VB code myself, and save, I'll get an error message that some chars won't be saved, then the chars in question will become little boxes in the source code. But then when I run the app, it splits the text correctly!
Problem is each file has a unique delimiter, located at the same position in the file (to define it) and I have to be able to get that code into the text and have it compare correctly in order to split the data over it.
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|