Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / May 2004

Tip: Looking for answers? Try searching our database.

StreamWriter Adds FFFE at the start of the file with Unicode Encoding!!

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Majed - 21 May 2004 02:00 GMT
Hi , all
I'm trying to write unicode to a file for another app (not developed with
vs2003) to read it. I used StreamWriter with unicode encoding.but I was
surprised that the streamwriter adds FFFE to the start of the file,which
stopes the other app from reading it!!
any idea how to stope it frome doing that,do I have to use another class
#####writer that supports unicode?

help me Please!
Thanks
majed
I did some code with diferent encoding and here is the result:
UTF7                             48656C6C6F2B4143454149512D0D0A
Hello+ACEAIQ-..
DEFAULT                      48656C6C6F21210D0A
Hello!!..
ASCII                             48656C6C6F21210D0A
Hello!!..
BIGENDIANUNICODE  FEFF00480065006C006C006F00210021000D000A
...H.e.l.l.o.!.!....
UNICODE                      FFFE480065006C006C006F00210021000D000A00
..H.e.l.l.o.!.!.....
UTF8                              EFBBBF48656C6C6F21210D0A
...Hello!!..

and here is the code

Imports System

Imports System.Text

Imports System.IO

Class filestream

Public Shared Sub main()

Dim ASCIIStream As New StreamWriter("ASCIIStream.txt", False,
System.Text.Encoding.ASCII)

Dim BigEndianUnicodeStream As New StreamWriter("BigEndianUnicodeStream.txt",
False, System.Text.Encoding.BigEndianUnicode)

Dim UnicodeStream As New StreamWriter("UnicodeStream.txt", False,
System.Text.Encoding.Unicode)

Dim DefaultStream As New StreamWriter("DefaultStream.txt", False,
System.Text.Encoding.Default)

Dim UTF7Stream As New StreamWriter("UTF7Stream.txt", False,
System.Text.Encoding.UTF7)

Dim UTF8Stream As New StreamWriter("UTF8Stream.txt", False,
System.Text.Encoding.UTF8)

ASCIIStream.WriteLine("Hello!!")

ASCIIStream.Close()

ASCIIStream = Nothing

BigEndianUnicodeStream.WriteLine("Hello!!")

BigEndianUnicodeStream.Close()

BigEndianUnicodeStream = Nothing

UnicodeStream.WriteLine("Hello!!")

UnicodeStream.Close()

UnicodeStream = Nothing

DefaultStream.WriteLine("Hello!!")

DefaultStream.Close()

DefaultStream = Nothing

UTF7Stream.WriteLine("Hello!!")

UTF7Stream.Close()

UTF7Stream = Nothing

UTF8Stream.WriteLine("Hello!!")

UTF8Stream.Close()

UTF8Stream = Nothing

Console.WriteLine ("Done!)

End Sub

End Class
Jay B. Harlow [MVP - Outlook] - 21 May 2004 02:16 GMT
Majed,
Sounds like a bug in your other app!

FFFE is the byte order mark of Unicode, it signifies to Unicode that the
bytes are little endian. While FEFF signifies to Unicode that the bytes are
big endian!

Notice that in your samples, FEFF is Big Endian
> BIGENDIANUNICODE  FEFF00480065006C006C006F00210021000D000A
> ...H.e.l.l.o.!.!....

, while FFFE is little endian:
> UNICODE                      FFFE480065006C006C006F00210021000D000A00
> ..H.e.l.l.o.!.!.....

You can prevent the byte order mark from being written by creating a new
UnicodeEncoding object that you pass as the Encoding parameter of the
STreamWriter constructor.

Something like(untested):

   ' little endian, no byte order marks
   Dim encoding As New UnicodeEncoding(False, False)

   Dim stream as FileStream
   Dim writer As New StreamWriter(stream, encoding)

Of course you will need to be certain that you other app does not have an
issue with little endian verses big endian Unicode characters.

Hope this helps
Jay

> Hi , all
> I'm trying to write unicode to a file for another app (not developed with
[quoted text clipped - 92 lines]
>
> End Class
Majed - 20 May 2004 12:40 GMT
hi jay
the other app is Ntbackup which saves its backup files list in unicode
without FFFE at the start of the file. any idea how can I remove it from the
file.
TIA
> Majed,
> Sounds like a bug in your other app!
[quoted text clipped - 8 lines]
>
> , while FFFE is little endian:
Jay B. Harlow [MVP - Outlook] - 21 May 2004 05:24 GMT
Majed,
> any idea how can I remove it from the
> file.
I take it you did not read my response! As I indicated in my response how
you remove it from the file!

Hope this helps
Jay

> hi jay
> the other app is Ntbackup which saves its backup files list in unicode
[quoted text clipped - 14 lines]
> >
> > , while FFFE is little endian:
Majed - 20 May 2004 22:07 GMT
jay,
i'm sorry about that,I was late up because of this problem. yuor reply was
great and did solve it.
tahnks and sorry again.:)
majed
> Majed,
> > any idea how can I remove it from the
[quoted text clipped - 24 lines]
> > >
> > > , while FFFE is little endian:

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.