Pound symbol in a string

P

Paul Hadfield

All,

When I use system.IO.StreamWriter to write append a string to file that
contains the GBP pound symbol, I notice that it also appends an extra
character before the pond symbol. Yet when I examine the string character by
character in the command window, the extra character is not there.

For example:

Dim Line As New StringBuilder
Line.Append("Cost=£10")

Dim SW As StreamWriter
SW = File.AppendText("C:\Test.txt")

SW.WriteLine(Line(0))

SW.Close()

If I then view the contents of the file in notepad it shows:
Cost=£10

If I use a hex editor to view the contents of the file it shows:
43 6F 73 74 3D C2 A3 31 30 0D 0A

Obviously the 0D and 0A at the end are the Cr+Lf charactors, but I don't
understand where the C2 charactor has come from??


Sorry if this is a silly question - I'm really only used to VB6 - still
trying to get my head around dot net.

Cheers,

Paul.
 
H

Herfried K. Wagner [MVP]

Paul,

Paul Hadfield said:
When I use system.IO.StreamWriter to write append a string to file that
contains the GBP pound symbol, I notice that it also appends an extra
character before the pond symbol. Yet when I examine the string character
by character in the command window, the extra character is not there.

For example:

Dim Line As New StringBuilder
Line.Append("Cost=£10")

Dim SW As StreamWriter
SW = File.AppendText("C:\Test.txt")

SW.WriteLine(Line(0))
SW.Close()

If I then view the contents of the file in notepad it shows:
Cost=£10

If I use a hex editor to view the contents of the file it shows:
43 6F 73 74 3D C2 A3 31 30 0D 0A

Obviously the 0D and 0A at the end are the Cr+Lf charactors, but I don't
understand where the C2 charactor has come from??

'StreamWriter' uses UTF-8 as the default encoding. You can change the
encoding used to encode the text written to the file by specifying one of
the 'System.Text.Encoding.*' encodings.
 
J

Jay B. Harlow [MVP - Outlook]

Paul,
As Herfried suggests StreamWriter by default uses UTF8. Normally you want to
use the encoding as specific under your regional settings in Control Panel,
which is Encoding.Default.

Something like:

Imports System.Text

Dim writer As New StreamWriter("Test.txt", True, Encoding.Default)

writer.WriteLine("Cost=£10")

writer.Close()

Hope this helps
Jay
 

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