473,659 Members | 3,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting around .Net Strings being UTF-16 encoded only

Hi I have the following issue.

I have a character that is return by a SQL Server database "É" to be
precise, the issue is that when I store character in a .net string
variable my understanding is that it converts it to UTF-16
automatically. I've seen a lot of quotes saying "All String datatype
variables in .NET are UTF-16 encoded". The thing is that I want to
write this character to a XML file in UTF-8 encoded format instead. How
do I go about doing this?

Right now I've tried the following scenerio's (all in VB.Net) when
writing to my XmlTextWriter object:

1) Encoding the string into UTF-8 bytes, then placing the string into
a Stream Reader and then from there reading the stream into the
XmlTextWriter

Dim utf8 As New UTF8Encoding(Tr ue)
Dim fNameBytes As Byte() = utf8.GetBytes(s trFName)

Dim stmFName As StreamReader = _
New StreamReader( New MemoryStream( fNameBytes ), utf8)

....

EFSXml_UserInfo .WriteAttribute String("firstna me",
stmFName.ReadTo End() )

2) I've also tried the code below:

Dim utf8 As New UTF8Encoding(Tr ue)

Dim xtrFName As XmlTextReader = _
New XmlTextReader(N ew StringReader(st rFName))
Dim msFName As MemoryStream = New MemoryStream()

Dim xtwFName As XmlTextWriter = New XmlTextWriter(m sFName, utf8)

xtwFName.WriteN ode(xtrFName, false)
xtwFName.Flush( )

msFName.Positio n = 0

Dim srFName As StreamReader = New StreamReader(ms FName)

....

EFSXml_UserInfo .WriteAttribute String("firstna me", srFName
..ReadToEnd() )

Neither of the 2 result in encoding the string correctly, can someone
please help me out, I've spend close to 2 days trying to figure this
stupid thing out.

Thanks,

Mike Murray

Nov 1 '05 #1
5 3270
Mike,

Your name sounds that you are in the uni code 1252 area, wich is huge.

http://www.geocities.com/Athens/Acad.../fontset.htm#b

You should only be busy with encoding when you know that you get characters
from outside that area.

I hope this helps,

Cor
"Mike Murray" <ou*********@gm ail.com> schreef in bericht
news:11******** *************@f 14g2000cwb.goog legroups.com...
Hi I have the following issue.

I have a character that is return by a SQL Server database "É" to be
precise, the issue is that when I store character in a .net string
variable my understanding is that it converts it to UTF-16
automatically. I've seen a lot of quotes saying "All String datatype
variables in .NET are UTF-16 encoded". The thing is that I want to
write this character to a XML file in UTF-8 encoded format instead. How
do I go about doing this?

Right now I've tried the following scenerio's (all in VB.Net) when
writing to my XmlTextWriter object:

1) Encoding the string into UTF-8 bytes, then placing the string into
a Stream Reader and then from there reading the stream into the
XmlTextWriter

Dim utf8 As New UTF8Encoding(Tr ue)
Dim fNameBytes As Byte() = utf8.GetBytes(s trFName)

Dim stmFName As StreamReader = _
New StreamReader( New MemoryStream( fNameBytes ), utf8)

....

EFSXml_UserInfo .WriteAttribute String("firstna me",
stmFName.ReadTo End() )

2) I've also tried the code below:

Dim utf8 As New UTF8Encoding(Tr ue)

Dim xtrFName As XmlTextReader = _
New XmlTextReader(N ew StringReader(st rFName))
Dim msFName As MemoryStream = New MemoryStream()

Dim xtwFName As XmlTextWriter = New XmlTextWriter(m sFName, utf8)

xtwFName.WriteN ode(xtrFName, false)
xtwFName.Flush( )

msFName.Positio n = 0

Dim srFName As StreamReader = New StreamReader(ms FName)

....

EFSXml_UserInfo .WriteAttribute String("firstna me", srFName
..ReadToEnd() )

Neither of the 2 result in encoding the string correctly, can someone
please help me out, I've spend close to 2 days trying to figure this
stupid thing out.

Thanks,

Mike Murray
Nov 2 '05 #2
Mike Murray <ou*********@gm ail.com> wrote:
I have a character that is return by a SQL Server database "É" to be
precise, the issue is that when I store character in a .net string
variable my understanding is that it converts it to UTF-16
automatically. I've seen a lot of quotes saying "All String datatype
variables in .NET are UTF-16 encoded". The thing is that I want to
write this character to a XML file in UTF-8 encoded format instead. How
do I go about doing this?


Just pass the XmlTextWriter constructor a stream and Encoding.UTF8.

I suspect if that doesn't work, you aren't getting the data back
properly in the first place.

See http://www.pobox.com/~skeet/csharp/unicode.html and
http://www.pobox.com/~skeet/csharp/d...ngunicode.html

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 2 '05 #3
It should be easier by using the constructor that allows to specify the
stream encoding...

I would first try with a simple text file in particular :
- paste this char into notepad and save the file as UTF-8
- create this file programmaticaly

You can then use "debug" to compare those files bytes per bytes (should be
easy with 4 bytes !). For now it could range from the character not being
stored correctly in the db to the tool used to read back the file to see if
it worked that wouldn't take into account that this is an UTF-8 encoded file
making appear it bad when it is correct etc...

--
Patrice

"Mike Murray" <ou*********@gm ail.com> a écrit dans le message de
news:11******** *************@f 14g2000cwb.goog legroups.com...
Hi I have the following issue.

I have a character that is return by a SQL Server database "É" to be
precise, the issue is that when I store character in a .net string
variable my understanding is that it converts it to UTF-16
automatically. I've seen a lot of quotes saying "All String datatype
variables in .NET are UTF-16 encoded". The thing is that I want to
write this character to a XML file in UTF-8 encoded format instead. How
do I go about doing this?

Right now I've tried the following scenerio's (all in VB.Net) when
writing to my XmlTextWriter object:

1) Encoding the string into UTF-8 bytes, then placing the string into
a Stream Reader and then from there reading the stream into the
XmlTextWriter

Dim utf8 As New UTF8Encoding(Tr ue)
Dim fNameBytes As Byte() = utf8.GetBytes(s trFName)

Dim stmFName As StreamReader = _
New StreamReader( New MemoryStream( fNameBytes ), utf8)

....

EFSXml_UserInfo .WriteAttribute String("firstna me",
stmFName.ReadTo End() )

2) I've also tried the code below:

Dim utf8 As New UTF8Encoding(Tr ue)

Dim xtrFName As XmlTextReader = _
New XmlTextReader(N ew StringReader(st rFName))
Dim msFName As MemoryStream = New MemoryStream()

Dim xtwFName As XmlTextWriter = New XmlTextWriter(m sFName, utf8)

xtwFName.WriteN ode(xtrFName, false)
xtwFName.Flush( )

msFName.Positio n = 0

Dim srFName As StreamReader = New StreamReader(ms FName)

....

EFSXml_UserInfo .WriteAttribute String("firstna me", srFName
..ReadToEnd() )

Neither of the 2 result in encoding the string correctly, can someone
please help me out, I've spend close to 2 days trying to figure this
stupid thing out.

Thanks,

Mike Murray
Nov 2 '05 #4
Hi folks,

Here is some more information.

The application that is expecting the UTF-8 Encoding creates a file
using the the default that comes in from the web browser and creates
the file via vbscript, windows scripting code. Right now there is no
conversion to UTF-8 happening in the vbscript, the programmer is just
relying on what comes in through the browser to the vbscript (is there
anything funny with variables storing the incoming text as UTF-16 just
like .NET does?)

What I want to do is create the same file but from vb.NET, therefore
that is why i'm using the XmlTextWriter code. Which makes sense to me,
I just want to clarify that my code is correct, and i'm going about
this the right way.

Thanks,

Mike

Nov 3 '05 #5
Mike Murray <ou*********@gm ail.com> wrote:
Here is some more information.

The application that is expecting the UTF-8 Encoding creates a file
using the the default that comes in from the web browser and creates
the file via vbscript, windows scripting code. Right now there is no
conversion to UTF-8 happening in the vbscript, the programmer is just
relying on what comes in through the browser to the vbscript (is there
anything funny with variables storing the incoming text as UTF-16 just
like .NET does?)
By the time you get the string, it should be correct Unicode.
What I want to do is create the same file but from vb.NET, therefore
that is why i'm using the XmlTextWriter code. Which makes sense to me,
I just want to clarify that my code is correct, and i'm going about
this the right way.


It sounds okay to me. You shouldn't need to do anything to get
XmlTextWriter to create files correctly in UTF-8.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 4 '05 #6

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

Similar topics

11
2934
by: Laurent Therond | last post by:
Maybe you have a minute to clarify the following matter... Consider: --- from cStringIO import StringIO def bencode_rec(x, b): t = type(x)
4
4293
by: Joerg Lehmann | last post by:
I am using Python 2.2.3 (Fedora Core 1). The problem is, that strings containing umlauts do not work as I would expect. Here is my example: >>> a = 'äöü' >>> b = '123' >>> print "%-5s %-5s\n%-5s %-5s" % (a,a,b,b) äöü äöü 123 123 I would expect, that the displayed width of a or b is the same: 5 characters.
97
4400
by: Kjetil Torgrim Homme | last post by:
often when re-factoring code, I need to change the indent level of some chunk of code. due to the lack of an end marker, my Emacs has to use heuristics when re-indenting, and this will occasionally lead to mistakes. of course this is really _my_ fault, not Emacs', but it's annoying all the same. a colleague uses #fi, #yrt etc. to mark the end of blocks, but I don't find this satisfactory since neither Python nor Emacs has any...
9
3694
by: Ksenia Marasanova | last post by:
Hi, I have a little problem with encoding. Was hoping maybe anyone can help me to solve it. There is some amount of data in a database (PG) that must be inserted into Excel sheet and emailed. Nothing special, everything works. Except that non-ascii characters are not displayed properly. The data is stored as XML into a text field. When I use pgsql it's displayed good in the terminal. Now I run my script and print data
1
1933
by: Ziemowit Skowronski | last post by:
Hi, I have a problem with displaying unicode strings in ASP.NET (C#). I recieve data from MS SQL Server database and use them to construct navigation on the page. Because I'm from Poland, I have to display Polish accent characters and here is a problem. On my testing/developing server (Windows 2003 Server) all works fine, but on production server (Windows 2000 Server) Polish accents are missing. Database collation is set to...
29
3509
by: Ron Garret | last post by:
>>> u'\xbd' u'\xbd' >>> print _ Traceback (most recent call last): File "<stdin>", line 1, in ? UnicodeEncodeError: 'ascii' codec can't encode character u'\xbd' in position 0: ordinal not in range(128) >>>
5
4589
by: robson.cozendey.rj | last post by:
Hi, I´m trying desperately to tell the interpreter to put an 'á' in my string, so here is the code snippet: # -*- coding: utf-8 -*- filename = u"Ataris Aquáticos #2.txt" f = open(filename, 'w') Then I save it with Windows Notepad, in the UTF-8 format. So:
3
2220
by: 7stud | last post by:
Can anyone tell me why I can print out the individual variables in the following code, but when I print them out combined into a single string, I get an error? symbol = u'ibm' price = u'4 \xbd' # 4 1/2 print "%s" % symbol print "%s" % price.encode("utf-8") print "%s %s" % (symbol, price.encode("utf-8") )
6
1872
by: | last post by:
Hi, I used extensively python and now I find this mess with strings, I can't even reproduce tutorial examples: File "<stdin>", line 0 ^ SyntaxError: 'ascii' codec can't decode byte 0xc4 in position 1: ordinal not in range(128) Is there any good guide to this mess of codecs and hell ?
5
5562
by: Asterix | last post by:
how could I test that those 2 strings are the same: 'séd' (repr is 's\\xc3\\xa9d') u'séd' (repr is u's\\xe9d')
0
8428
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
8335
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,...
0
8627
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...
0
7356
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
6179
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
4175
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
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
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
1737
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.