473,944 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to UTF-8 encode a string?


hello,
i'm doing utf-8 encoding the following way.
string message;

UTF8Encoding utf8 = new UTF8Encoding();

Byte[] encodedBytes = utf8.GetBytes(m essage);

message = encodedBytes.To String();

can someone correct me?

many thanks

JJ
Apr 24 '06
28 95165

"Mike Schilling" <ap@newsgroup.n ospam> skrev i en meddelelse
news:uH******** ******@TK2MSFTN GP02.phx.gbl...
I am presuming that the problem is that the bytes are correct but the
remote service doesn't recognize them as being UTF-8. Try adding

req.ContentEnco ding = encoding;


My request object "req" does not seem to have seem to have "ContentEncodin g"
property.
I'm i missing something?



Apr 24 '06 #11
Hello, jens!

try
req.ContentType = "text/xml; charset=utf-8";

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Apr 24 '06 #12
> try
req.ContentType = "text/xml; charset=utf-8";

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com

Hi Vadym,

I applied this, no chenge in the reponse from the server.

How can i actually dump the utf-8 encoded content to a file so i can see it
actually format following utf-8?

Many thanks

JJ
Apr 24 '06 #13
Jon Skeet wrote:
[...snip...]
Note that you can use Encoding.UTF8En coding to avoid creating a new
instance each time.

[...snip...]

Would that code really create a new instance? I'd expect it to be a
Singleton or something like that, since it does not store any state
information.
Apr 24 '06 #14
Hello, jens!

jJ> How can i actually dump the utf-8 encoded content to a file so i can
jJ> see it actually format following utf-8?

UTF8Encoding encoding = new UTF8Encoding();
byte[] postBytes = encoding.GetByt es(message);
req.ContentLeng th = postBytes.Lengt h;
System.IO.Strea m reqStream = req.GetRequestS tream();
reqStream.Write (postBytes, 0, postBytes.Lengt h);
reqStream.Close ();

System.IO.File. WriteAllBytes(p athHere, postBytes);


--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Apr 24 '06 #15
Hello, jens!

jJ> I applied this, no chenge in the reponse from the server.

What is server response? what error code? any content returned?

Another question if remote end is a webservice, why can't you make web reference to it and then communicate via stub?
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Apr 24 '06 #16
Michael Voss wrote:
Jon Skeet wrote:
[...snip...]
Note that you can use Encoding.UTF8En coding to avoid creating a new
instance each time.

[...snip...]

Would that code really create a new instance? I'd expect it to be a
Singleton or something like that, since it does not store any state
information.


Using Encoding.UTF8En coding would reuse a single instance (I believe)
which is why I was suggesting using that instead of new UTF8Encoding().

Jon

Apr 24 '06 #17

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:11******** *************@v 46g2000cwv.goog legroups.com...
Michael Voss wrote:
Jon Skeet wrote:
[...snip...]
> Note that you can use Encoding.UTF8En coding to avoid creating a new
> instance each time.

[...snip...]

Would that code really create a new instance? I'd expect it to be a
Singleton or something like that, since it does not store any state
information.


Using Encoding.UTF8En coding would reuse a single instance (I believe)
which is why I was suggesting using that instead of new UTF8Encoding().


Firstly there is no way to make "new X()" return an existing object because
unlike C++ you cannot override new so you can't do secret singletons.

Secondly the encoding does store 'state' information in the form of the
settings for BOM and exception throwing so in 1.1 there are 4 possible UTF8
encoding objects.

Worse, in 2.0 there is a settable EncoderFallback property which makes all
instances potentially unshareable. The work around for the static properties
is that they are all made readonly (IsReadOnly) and throw
InvalidOperatio nException if you try to set them. [It doesn't actually say
that in the documentation but it's the only sensible thing to do]

Also the static property is actually called Encoding.UTF8
Apr 24 '06 #18
There is a serious documentation issue with the static encoding properties:

BOM and exception handling are undefined and therefore these properties are
not usable without firstly investigating their actual behaviour and secondly
taking it on faith that MS wont change it.

1) The documentaion should state what these are set to.
2) There should be properties to show these settings.
3) The documentation should state that they are readonly (in fact all
documentation should state the default value for all properties but it
rarely does)

In the absense of documentation the only easily maintainable way to go is to
always create your own using UTF8Encoding(bo ol,bool). The cost should be
trivial.

Apr 24 '06 #19
Nick Hounsome wrote:
Using Encoding.UTF8En coding would reuse a single instance (I believe)
which is why I was suggesting using that instead of new UTF8Encoding().
Firstly there is no way to make "new X()" return an existing object because
unlike C++ you cannot override new so you can't do secret singletons.


Unless of course you're the CLR :)

(As discussed a short while ago, the System.String constructor returns
String.Empty in certain circumstances.)
Secondly the encoding does store 'state' information in the form of the
settings for BOM and exception throwing so in 1.1 there are 4 possible UTF8
encoding objects.
True - although it's not mutable state. It's not the kind of state
which prevents something being reused, let's say. (I want a word for
this kind of state - I've been using it a lot recently.)
Worse, in 2.0 there is a settable EncoderFallback property which makes all
instances potentially unshareable.
Aargh. I take back the above :)
The work around for the static properties
is that they are all made readonly (IsReadOnly) and throw
InvalidOperatio nException if you try to set them. [It doesn't actually say
that in the documentation but it's the only sensible thing to do]
Not sure what you mean - the Encoding.XXX properties? Or the properties
on the objects returned by the Encoding.XXX properties?
Also the static property is actually called Encoding.UTF8


Oops, yes.

Jon

Apr 24 '06 #20

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

Similar topics

3
3391
by: aa | last post by:
Is it OK to include an ANSI file into a UTF-8 file?
27
5176
by: EU citizen | last post by:
Do web pages have to be created in unicode in order to use UTF-8 encoding? If so, can anyone name a free application which I can use under Windows 98 to create web pages?
38
5772
by: Haines Brown | last post by:
I'm having trouble finding the character entity for the French abbreviation for "number" (capital N followed by a small supercript o, period). My references are not listing it. Where would I find an answer to this question (don't find it in the W3C_char_entities document). -- Haines Brown brownh@hartford-hwp.com
48
4682
by: Zenobia | last post by:
Recently I was editing a document in GoLive 6. I like GoLive because it has some nice features such as: * rewrite source code * check syntax * global search & replace (through several files at once) * regular expression search & replace. Normally my documents are encoded with the ISO setting. Recently I was writing an XHTML document. After changing the encoding to UTF-8 I used the
16
6193
by: lawrence | last post by:
I was told in another newsgroup (about XML, I was wondering how to control user input) that most modern browsers empower the designer to cast the user created input to a particular character encoding. This arose in answer to my question about how to control user input. I had complained that I had users who wrote articles in Microsoft Word or WordPerfect and then input that to the web through a textarea box on a form I'd created. I've...
1
15636
by: stevelooking41 | last post by:
Can someone explain why I don't seem unable to use document.write to produce a valid UTF-8 none breaking space sequence (Hex: C2A0) ? I've tried everyway I've been able to find to tell the browser I'm trying to print UTF-8 and still no luck. I'd like the first 2 tries to match the second two tries as far as output. <HTML> <meta http-equiv="Content-Type" content="application/x-script; charset=UTF-8">
1
2027
by: David Bertoni | last post by:
Hi all, I'm trying to resolve what appears to me an inconsistency in the XML 1.0 recommendation involving entities encoding in UTF-16 and the requirement for a byte order mark. Section 4.3.3 has the following text: http://www.w3.org/TR/REC-xml/#charencoding
7
12178
by: Jimmy Shaw | last post by:
Hi everybody, Is there any SIMPLE way to convert from UTF-16 to UTF-32? I may be mixed up, but is it possible that all UTF-16 "code points" that are 16 bits long appear just the same in UTF-32, but with zero padding and hence no real conversion is necessary? If I am completely wrong and some intricate conversion operation needs to take place, can anyone give me some primer on the subject?
10
19616
by: Jed | last post by:
I have a form that needs to handle international characters withing the UTF-8 character set. I have tried all the recommended strategies for getting utf-8 characters from form input to email message and I cannot get it to work. I need to stay with classic asp for this. Here are some things I tried: 'CDONTS Call msg.SetLocaleIDs(65001)
35
4385
by: Bjoern Hoehrmann | last post by:
Hi, For a free software project, I had to write a routine that, given a Unicode scalar value U+0000 - U+10FFFF, returns an integer that holds the UTF-8 encoded form of it, for example, U+00F6 becomes 0x0000C3B6. I came up with the following. I am looking for a more elegant solution, that is, roughly, faster, shorter, more readable, ... while producing the same ouput for the cited range. unsigned int
0
10147
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10677
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8238
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7399
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4519
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3520
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.