473,749 Members | 2,432 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Write utf8 encoded string


Hi

I am trying to write to a string text encoded to utf8 as oppose to utf16

Since the data comes from an XML object (and I serialize it) I need to pass
either StreamWriter or a StringWriter object, I don't want to create a file
so I want to use a StringWriter (passing to it's constructor a
StringBuilder) The problem is that the StringWriter encodes utf16 (I don't
know how to change it)

Any Advice?

Thank you in advance,
Samuel
Jul 30 '08 #1
4 2518
Samuel wrote:
I am trying to write to a string text encoded to utf8 as oppose to utf16

Since the data comes from an XML object (and I serialize it) I need to pass
either StreamWriter or a StringWriter object, I don't want to create a file
so I want to use a StringWriter (passing to it's constructor a
StringBuilder) The problem is that the StringWriter encodes utf16 (I don't
know how to change it)
No, a StringWriter doesn't encode anything at all. What you write to it
ends up in the internal StringBuilder as characters, not encoded as bytes.

Why do you think that the StringWriter would encode the text? How are
you using it?

--
Göran Andersson
_____
http://www.guffa.com
Jul 30 '08 #2
"Samuel" <sa************ @ntlworld.comsc hrieb:
I am trying to write to a string text encoded to utf8 as oppose to utf16

Since the data comes from an XML object (and I serialize it) I need to
pass either StreamWriter or a StringWriter object, I don't want to create
a file so I want to use a StringWriter (passing to it's constructor a
StringBuilder) The problem is that the StringWriter encodes utf16 (I don't
know how to change it)
Strings in .NET are always encoded in UTF-16 when stored in memory. You
cannot change that. If you want to get the byte representation of a string
in a certain encoding, check 'System.Text.En coding.GetBytes '.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jul 30 '08 #3
The String type deals with utf16 strings only. Of course you can use an utf8
encoding when reading/writing from/to an external destination (such as a
file or a web browser response stream).

Not sure why you want to keep the string using utf8 but I'm afraid that
depending on what you'll do with the string it could well introduce some
subtil problems (I would keep it a s abyte array in the worst case if
possible)...

You may want to elaborate about what you are trying to do and in particular
what you'll do with this string once encoded using utf8 (for example even
with creating mails the idea is that .NET always uses utf16 but you can ask
to encode as utf8 when you write youtr content to the mail object, a file, a
browser stream, a database etc...)
--
Patrice

"Samuel" <sa************ @ntlworld.coma écrit dans le message de groupe de
discussion : Oa************* *@TK2MSFTNGP03. phx.gbl...
>
Hi

I am trying to write to a string text encoded to utf8 as oppose to utf16

Since the data comes from an XML object (and I serialize it) I need to
pass either StreamWriter or a StringWriter object, I don't want to create
a file so I want to use a StringWriter (passing to it's constructor a
StringBuilder) The problem is that the StringWriter encodes utf16 (I don't
know how to change it)

Any Advice?

Thank you in advance,
Samuel

Jul 31 '08 #4
>>THIS MAY NOT BE THE ACTUAL ENCODING

Herfried wrote:
STRINGS IN .NET ARE ALWAYS ENCODED IN UTF-16 WHEN STORED IN MEMORY
Aug 1 '08 #5

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

Similar topics

1
3972
by: ryang | last post by:
I am trying to understand how to work with Unicode in Perl. I have read the relevant man pages (perluniintro, perlunicode, etc.) and have written severl scripts to test/verifiy my understanding. However, I created a script that has unexpected output. The script is below and it contains some UTF-8 encoded characters which represent all five Spanish accented vowels plus the enye (n with a tilde over it) in upper and lower case. I hope...
2
3275
by: Chris Mullins | last post by:
I've spent a bit of time over the last year trying to implement RFC 3454 (Preparation of Internationalized Strings, aka 'StringPrep'). This RFC is also a dependency for RFC 3491 (Internationalized Domain Names / IDNA) which is something that I also need to support. The problem that I've been struggling with in .NET is that of Unicode Code Points > 0xFFFF. These points are encoded into UTF8 using the Surrogate Pair encoding scheme that...
9
24208
by: Thomas Podlesak | last post by:
I need a check, if a file is utf8 encoded. I only found the php-functions 'iconv' and 'recode'. But it seems it´s not possible to determine the encoding with them. Isn´t there any similar function to the 'file'-command on linux for php?
4
2416
by: chris_fieldhouse | last post by:
Hi, I'm almost done with a php driven email filter and automated forwarder, I've tested it out with various emails and ironed out plain text and html. But this final item has me stumped. When processing an email which contains UTF8 encoded characters, I can't work out how to detect the presence of the UTF8 characters, so I get =E2=80=99 displayed instead of a '.
4
5224
by: Peter Münster | last post by:
Hello, str_word_count() does not seem to work with locale "fr_FR.utf8". The output of the following script is string(10) "fr_FR.utf8" Array ( =bi =re ) I think, that "bière" should be recognized as word. Here is the test-script:
7
13103
by: amygdala | last post by:
Hi, I'm trying to let PHP write a 'sitemap.xml' sitemap for Google and other searchengines. It's working, except that the content in the XML file doesn't seem to be UTF8. (Which it should be, judging by the information given on Google's webmaster helpcenter). The way I test to see if the content is UTF8, is by opening the XML file in notepad and choose 'save as...'. Normally the coding option should be set to UTF8, but now it just...
0
1010
by: Tim Golden | last post by:
Lawrence, Anna K (US SSA) wrote: From where I'm sitting, I can't see enough to help. The crucial thing seems to be in this phrase: "When I try to take a string that is coming out of the database and export it to a Word document, no amount of encoding/decoding will make the Word doc display properly". It presumably doesn't matter what web framework etc. you're using: you either get a unicode object or an encoded string from the database...
3
2186
by: dmitrey | last post by:
hi all, what's the best way to write Python dictionary to a file? (and then read) There could be unicode field names and values encountered. Thank you in advance, D.
10
2224
by: Simon Willison | last post by:
I'm having a horrible time trying to get xml.dom.pulldom to consume a UTF8 encoded XML file. Here's what I've tried so far: <msg>Simon\xe2\x80\x99s XML nightmare</msg> """ ('START_DOCUMENT', <xml.dom.minidom.Document instance at 0x6f06c0>) ('START_ELEMENT', <DOM Element: msg at 0x6f0710>) .... UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 21: ordinal not in range(128)
0
8997
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
8833
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8257
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6801
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
4709
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
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3320
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
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.