Results 1 to 7 of 7

Thread: Can't get this code to display £ chars

  1. #1

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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:
    1. If ofd.ShowDialog = DialogResult.OK Then
    2.             If File.Exists(ofd.FileName) Then
    3.  
    4.                 Dim sr As StreamReader = New StreamReader(ofd.FileName)
    5.                 Try
    6.                     txtOriginal.Text = sr.ReadToEnd
    7.                 Catch ex As Exception
    8.                     MessageBox.Show("Could not load that file: " & ex.Message, "Censor", MessageBoxButtons.OK)
    9.                 Finally
    10.                     'tidy up a bit
    11.                     txtOriginal.SelectionLength = 0
    12.                     sr.Close()
    13.                 End Try
    14.             End If
    15.         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.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    hehe... I tried everything except UTF7... figuring UTF8 must be just a more-feature rich encoding...

  4. #4

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    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.

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  6. #6
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    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

  7. #7
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    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
  •  



Click Here to Expand Forum to Full Width