473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Extended ASCII Characters - StreamWriter

I am using StreamWriter's .Writeline() method to create a simple text file. I
found that ASCII characters 255 & 254 are being inserted as the first two
characters of the file which causes a 3rd-party users' parser to fail. It is
not being inserted by my data as the first line starts with "LOGIN: ", i.e.
hard-coded.

Has anyone else encountered this? Is there any filtering settings I can
apply? I looked through the documentation on
Nov 16 '05 #1
4 7669
Has anyone else encountered this? Is there any filtering settings I can
apply? I looked through the documentation on


The first two bytes are probably the little-endian Unicode byte order
marker. Who creates the StreamWriter, and which encoding is it using?

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Not sure what you mean by who creates StreamWriter - it's provided in the C#
language. I am using the default "UTF8Encoding".
"Mattias Sjögren" wrote:
Has anyone else encountered this? Is there any filtering settings I can
apply? I looked through the documentation on


The first two bytes are probably the little-endian Unicode byte order
marker. Who creates the StreamWriter, and which encoding is it using?

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #3
Depending on how you create an instance of UTF8Encoding, you will get
different results. The encoding can contain a preamble, which identifies
the encoding used together with the encoded data. The
System.Encoding.UTF8 encoding always includes a preamble.

Using the code below you will get the output:

Stream length: 14
Stream length: 17
Stream length: 17

Which indicates that the first stream does not contain any preamble, but
the other two does.

Regards,
Joakim

using System;
using System.Text;
using System.IO;

public class App
{
public static void Main()
{
// Create an encoding without the preamble
MemoryStream ms1 = new MemoryStream();
StreamWriter w1 = new StreamWriter(ms1, new UTF8Encoding(false));
w1.Write("This is a test");
w1.Flush();
Console.WriteLine("Stream length: " + ms1.Length);

// Create an encoding specifically with the preamble
MemoryStream ms2 = new MemoryStream();
StreamWriter w2 = new StreamWriter(ms2, new UTF8Encoding(true));
w2.Write("This is a test");
w2.Flush();
Console.WriteLine("Stream length: " + ms2.Length);

// Use the default UTF8 encoding, which uses the preamble
MemoryStream ms3 = new MemoryStream();
StreamWriter w3 = new StreamWriter(ms3, Encoding.UTF8);
w3.Write("This is a test");
w3.Flush();
Console.WriteLine("Stream length: " + ms3.Length);
}
}

ASP Yaboh wrote:
Not sure what you mean by who creates StreamWriter - it's provided in the C#
language. I am using the default "UTF8Encoding".

Nov 16 '05 #4
Joakim,

Thank you - I will check into this.

"Joakim Karlsson" wrote:
Depending on how you create an instance of UTF8Encoding, you will get
different results. The encoding can contain a preamble, which identifies
the encoding used together with the encoded data. The
System.Encoding.UTF8 encoding always includes a preamble.

Using the code below you will get the output:

Stream length: 14
Stream length: 17
Stream length: 17

Which indicates that the first stream does not contain any preamble, but
the other two does.

Regards,
Joakim

using System;
using System.Text;
using System.IO;

public class App
{
public static void Main()
{
// Create an encoding without the preamble
MemoryStream ms1 = new MemoryStream();
StreamWriter w1 = new StreamWriter(ms1, new UTF8Encoding(false));
w1.Write("This is a test");
w1.Flush();
Console.WriteLine("Stream length: " + ms1.Length);

// Create an encoding specifically with the preamble
MemoryStream ms2 = new MemoryStream();
StreamWriter w2 = new StreamWriter(ms2, new UTF8Encoding(true));
w2.Write("This is a test");
w2.Flush();
Console.WriteLine("Stream length: " + ms2.Length);

// Use the default UTF8 encoding, which uses the preamble
MemoryStream ms3 = new MemoryStream();
StreamWriter w3 = new StreamWriter(ms3, Encoding.UTF8);
w3.Write("This is a test");
w3.Flush();
Console.WriteLine("Stream length: " + ms3.Length);
}
}

ASP Yaboh wrote:
Not sure what you mean by who creates StreamWriter - it's provided in the C#
language. I am using the default "UTF8Encoding".

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: | last post by:
Hey all, Quick question...been bugging me for some time, really. I have a console app, it does some things, and I want to save an array of text to a text file. The text consists of ASCII and...
13
by: bgbauer70 | last post by:
My appologies if this ends up being a duplicate post. For some reason the first post never showed up. I've tried about 300 iterrations of this same ability, and none of them seem to work in...
4
by: wob | last post by:
Many thanks for those who responded to my question of "putting greek char into C string". In searching for an solution, I noticed that there are more than one version of "Extended ASCII...
3
by: JSM | last post by:
Hi, I am just trying to port an existing simple encryption routine to C#. this routine simply adds/substracts 10 ascii characters to each character in a text file (except quotes). The routine...
4
by: =?Utf-8?B?Um9zaGFuIFIuRA==?= | last post by:
Hi All, I am new to C# programming; I am developing an application for recording audio data using voice modem. I am using HyperTerminal to manually process audio data. The modem when configured...
1
by: s123 | last post by:
Hi, while invoking a web service, if in xml request message i wrap the extended ASCII characters with CDATA it is not returning the desired result, while this is not the case if i do not wrap...
13
by: ramif | last post by:
Is there a way to print extended ASCII in C?? I tried to code something, but it only displays strange symbols. here is my code: main() { char chr = 177; //stores the extended ASCII...
0
by: jumperbl | last post by:
I am converting hex to ASCII and one of the words has a dot in the middle, which is in the extended ascii characters. when i do this i get the ascii chars. ...
5
by: siddhanta | last post by:
hi, i read the ascii value from a file and wrote the decimal one in other file. But values above "127" i.e. extended ascii are printed as" 65533." here ais wat i did: private void...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.