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

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(True)
Dim fNameBytes As Byte() = utf8.GetBytes(strFName)

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

....

EFSXml_UserInfo.WriteAttributeString("firstname",
stmFName.ReadToEnd() )

2) I've also tried the code below:

Dim utf8 As New UTF8Encoding(True)

Dim xtrFName As XmlTextReader = _
New XmlTextReader(New StringReader(strFName))
Dim msFName As MemoryStream = New MemoryStream()

Dim xtwFName As XmlTextWriter = New XmlTextWriter(msFName, utf8)

xtwFName.WriteNode(xtrFName, false)
xtwFName.Flush()

msFName.Position = 0

Dim srFName As StreamReader = New StreamReader(msFName)

....

EFSXml_UserInfo.WriteAttributeString("firstname", 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 3249
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*********@gmail.com> schreef in bericht
news:11*********************@f14g2000cwb.googlegro ups.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(True)
Dim fNameBytes As Byte() = utf8.GetBytes(strFName)

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

....

EFSXml_UserInfo.WriteAttributeString("firstname",
stmFName.ReadToEnd() )

2) I've also tried the code below:

Dim utf8 As New UTF8Encoding(True)

Dim xtrFName As XmlTextReader = _
New XmlTextReader(New StringReader(strFName))
Dim msFName As MemoryStream = New MemoryStream()

Dim xtwFName As XmlTextWriter = New XmlTextWriter(msFName, utf8)

xtwFName.WriteNode(xtrFName, false)
xtwFName.Flush()

msFName.Position = 0

Dim srFName As StreamReader = New StreamReader(msFName)

....

EFSXml_UserInfo.WriteAttributeString("firstname", 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*********@gmail.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.com>
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*********@gmail.com> a écrit dans le message de
news:11*********************@f14g2000cwb.googlegro ups.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(True)
Dim fNameBytes As Byte() = utf8.GetBytes(strFName)

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

....

EFSXml_UserInfo.WriteAttributeString("firstname",
stmFName.ReadToEnd() )

2) I've also tried the code below:

Dim utf8 As New UTF8Encoding(True)

Dim xtrFName As XmlTextReader = _
New XmlTextReader(New StringReader(strFName))
Dim msFName As MemoryStream = New MemoryStream()

Dim xtwFName As XmlTextWriter = New XmlTextWriter(msFName, utf8)

xtwFName.WriteNode(xtrFName, false)
xtwFName.Flush()

msFName.Position = 0

Dim srFName As StreamReader = New StreamReader(msFName)

....

EFSXml_UserInfo.WriteAttributeString("firstname", 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*********@gmail.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.com>
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
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
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...
97
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...
9
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....
1
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...
29
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...
5
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,...
3
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'...
6
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...
5
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
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: 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
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
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...
0
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...
0
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,...

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.