473,387 Members | 1,673 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,387 software developers and data experts.

System.Text.Encoding oddities

Sorry about the last... Anyway, here's the question:

I've been working on some C# routines to process strings in and out of various encodings. The hope is that I can just let the user type in the encoding they want and I'll do a pretty good job of converting. Basically, I take a string as input, write it to a byte array MemoryStream and then get the bytes of the conversion out.

The oddity in my question is that when I use System.Text.UTF8Encoding as an argument to my StreamWriter, I don't get the byteorder mark in the output, but when I use System.Text.Encoding.GetEncoding ("utf-8"); I do. Shouldn't these be the same, or am I missing something basic? Seems odd. Can someone explain why?

Thanks
-mark
Example code:
public byte [] Convert (string in, Encoding enc, out length)
{
MemoryStream out_stream = new MemoryStream(in.Length*3); // allow for encoding switch expansion
System.IO.StreamWriter writer = new System.IO.StreamWriter (out_stream, enc);
writer.Write (input);
writer.Flush(); // Flush but don't close, so we can get the MemoryStream used count

byte [] output = out_stream.GetBuffer();
length = out_stream.Length;
return output;
}

byte [] test = Convert ("test", System.Text.UTF8Encoding); // no bytemark
test = Convert ("test", System.Text.GetEncoding ("utf-8")); // bytemark
Nov 22 '05 #1
4 2791
Mark <ms********@lycos-inc.com> wrote:
Sorry about the last... Anyway, here's the question:

I've been working on some C# routines to process strings in and out
of various encodings. The hope is that I can just let the user type
in the encoding they want and I'll do a pretty good job of
converting. Basically, I take a string as input, write it to a byte
array MemoryStream and then get the bytes of the conversion out.

The oddity in my question is that when I use System.Text.UTF8Encoding
as an argument to my StreamWriter, I don't get the byteorder mark in
the output, but when I use System.Text.Encoding.GetEncoding
("utf-8"); I do. Shouldn't these be the same, or am I missing
something basic? Seems odd. Can someone explain why?


Well, it's basically not specified whether Encoding.UTF8 gives an
encoding with a byte order mark or not, or whether Encoding.GetEncoding
gives one with a BOM or not either.

If you want to make absolutely sure, you need to construct the
UTF8Encoding yourself, specifying whether or not you want a BOM as a
parameter.

However, there's a much easier way of doing conversion than creating a
StreamWriter - just call Encoding.GetBytes(string). That will never
contain a BOM.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #2
Mark <ms********@lycos-inc.com> wrote:
Sorry about the last... Anyway, here's the question:

I've been working on some C# routines to process strings in and out
of various encodings. The hope is that I can just let the user type
in the encoding they want and I'll do a pretty good job of
converting. Basically, I take a string as input, write it to a byte
array MemoryStream and then get the bytes of the conversion out.

The oddity in my question is that when I use System.Text.UTF8Encoding
as an argument to my StreamWriter, I don't get the byteorder mark in
the output, but when I use System.Text.Encoding.GetEncoding
("utf-8"); I do. Shouldn't these be the same, or am I missing
something basic? Seems odd. Can someone explain why?


Well, it's basically not specified whether Encoding.UTF8 gives an
encoding with a byte order mark or not, or whether Encoding.GetEncoding
gives one with a BOM or not either.

If you want to make absolutely sure, you need to construct the
UTF8Encoding yourself, specifying whether or not you want a BOM as a
parameter.

However, there's a much easier way of doing conversion than creating a
StreamWriter - just call Encoding.GetBytes(string). That will never
contain a BOM.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #3
Thanks..

Encoding.GetBytes() and Encoding.GetString () worked much better than my clunky approach - and the BOM (or lack thereof) is consistent...

That's a great help

-mar

Nov 22 '05 #4
Thanks..

Encoding.GetBytes() and Encoding.GetString () worked much better than my clunky approach - and the BOM (or lack thereof) is consistent...

That's a great help

-mar

Nov 22 '05 #5

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

Similar topics

2
by: Mark | last post by:
Sorry about the last... Anyway, here's the question: I've been working on some C# routines to process strings in and out of various encodings. The hope is that I can just let the user type in...
2
by: Mark | last post by:
Sorry about the last... Anyway, here's the question: I've been working on some C# routines to process strings in and out of various encodings. The hope is that I can just let the user type in...
0
by: Mark | last post by:
Sorry about the last... Anyway, here's the question: I've been working on some C# routines to process strings in and out of various encodings. The hope is that I can just let the user type in...
3
by: Tom | last post by:
I don't want to re-invent the wheel and am looking for a simple implementation of a text viewer or RichTextBox in read only mode that allows rapid file positioning within large data files without...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.