Writing a Unicode String into file

G

Guest

Hello,
I am trying to read the contents of 01.bin (unicode) into a String, to
modify it and finally to write it back into an other file named 02.bin.

If the file really contains "a b c", then everything is replaced properly.
But the finally output looks like "ÿþb b c".
There is that prefix ÿþ. I assume that it is always printed when you work
with unicode files but I do not know how to get rid of it.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim enc As System.Text.Encoding = System.Text.Encoding.Unicode

Dim a As String =
Microsoft.VisualBasic.FileIO.FileSystem.ReadAllText("01.bin", enc)

Dim b As String = Replace(a, "abc", "bbc", 1, -1)

Microsoft.VisualBasic.FileIO.FileSystem.WriteAllText("02.bin", b,
False, enc)

End Sub
 
H

Herfried K. Wagner [MVP]

kenny said:
I am trying to read the contents of 01.bin (unicode) into a String, to
modify it and finally to write it back into an other file named 02.bin.

If the file really contains "a b c", then everything is replaced properly.
But the finally output looks like "ÿþb b c".
There is that prefix ÿþ. I assume that it is always printed when you work
with unicode files but I do not know how to get rid of it.

It's the UTF-BOM. 'Encoding.Unicode' is actually UTF-16. You can avoid
creating the BOM as follows:

\\\
Dim enc As Encoding = New UnicodeEncoding(True, False)
Dim sw As New StreamWriter(..., enc)
///
 
T

tommaso.gastaldi

Do you (or others) know, by chance, if there is also a way to avoid
insertion of the Byte Order Mark (BOM), using the binary formatter? I
have alway been curious about that....

-tom

Herfried K. Wagner [MVP] ha scritto:
 

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