Home All Groups Group Topic Archive Search About

Writing a Unicode String into file

Author
15 Apr 2006 4:21 PM
kenny
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

Author
15 Apr 2006 5:19 PM
Cor Ligthert [MVP]
Kenny,

I think that you first have to decide in what code your outputfile should
be.

Some links,

General
http://www.microsoft.com/globaldev/reference/cphome.mspx

OS systems
http://www.microsoft.com/globaldev/reference/oslocversion.mspx

I hope this helps a little bit?

Cor

Show quoteHide quote
"kenny" <ke***@discussions.microsoft.com> schreef in bericht
news:1F5C6CB9-741B-4A16-A6BC-00D98C85210F@microsoft.com...
> 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
Author
15 Apr 2006 7:03 PM
Herfried K. Wagner [MVP]
"kenny" <ke***@discussions.microsoft.com> schrieb:
> 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)
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
15 Apr 2006 8:16 PM
kenny
thank you! it works.

@Cor Ligthert:
i think it should be Unicode :)

Show quoteHide quote
"Herfried K. Wagner [MVP]" wrote:

> 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)
> ///
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://classicvb.org/petition/>
>
Author
16 Apr 2006 1:10 PM
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:

Show quoteHide quote
> "kenny" <ke***@discussions.microsoft.com> schrieb:
> > 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)
> ///
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://classicvb.org/petition/>