Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2006

Tip: Looking for answers? Try searching our database.

C# ASP .NET -- UTF-16 encoding to UTF-8

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
davidjgonzalez@gmail.com - 28 Feb 2006 22:26 GMT
I have a web application written in ASP .NET (VS 2003) which an Adobe
Acrobat Form posts XML to. I am able to get the XML using the
Request.InputStream however the XML is UTF-16 encoded. This means that
the byte[] that i get from the Request.InputStream looks like:
[0]: 255
[1]: 254
[2]: 64
[3]: 0
[4]: 56
[5]: 0
...
essentially every other index in the array holds the value 0.
When i try to convert the byte array to a string, i get <\0x\0\m\0l\0
..  (every other character is a \0) .. I also have the '\r\n' character
before the ending tags in the xml.

my question is two fold

1) how can i elegantly convert the UTF-16 formated xml to something
more readable aka UTF-8, ASCII, etc in Visual Studio 2003 (i cant find
any UTF-16 encoding support in VS 2003)

2) If 1 doesnt get rid of the '\r\n's how can i get rid of them?
string.replace("\r\n", "") didnt seem to work.

Thanks
Greg Young - 28 Feb 2006 23:20 GMT
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fSystemTextUnicodeEncodingClassTopic.asp

use the GetString() method ... then if you want it in UTF 8 you can use
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fSystemTextUTF8EncodingClassTopic.asp
to convert it.

Greg

> I have a web application written in ASP .NET (VS 2003) which an Adobe
> Acrobat Form posts XML to. I am able to get the XML using the
[quoted text clipped - 22 lines]
>
> Thanks
davidjgonzalez@gmail.com - 01 Mar 2006 16:23 GMT
Greg thanks for the reply but that didn't seem to work..

My scope has slightly changed -- i no longer need UTF-8 encoding
persay, just need to parse the values from the XML that is being sent
over, so i need to convert the byte[] to a readable string.

.. here is my code:

-------------------------------------
Request.InputStream.Read(data, 0,
Convert.ToInt32(Request.InputStream.Length));

UnicodeEncoding encoding = new UnicodeEncoding( );
string decodedString = encoding.GetString(characters);
//at this point decodedString = " ????? ??????????? ?
?????????????????????????????????????????????????????????????????????????????????????????????????????????????"

DataSet ds = new DataSet();
ds = XmlToDataSet(decodedString);
-------------------------------------

when decodedString is passed to XmlToDataSet, it crashes I assume
because XmlToDataSet does not look like the encoding on the
decodedString..

How do i get the byte[] the Request.InputStream yields into a "normal"
encoding?

Thanks
Joerg Jooss - 01 Mar 2006 19:06 GMT
Thus wrote davidjgonzalez@gmail.com,

> Greg thanks for the reply but that didn't seem to work..
>
[quoted text clipped - 15 lines]
> ??????????????????????????????????????????????????????????????????????
> ???????????????????????????????????????"

Your code is not really complete. You're reading into a byte array "data",
but decode something called "characters".

Note that you don't really need to perform these steps yourself, if all you
want to do is fill a DataSet.

aDataSet.ReadXml(Request.InputStream);

should do the trick. The XML infrastructure can figure out the encoding by
itself.

Cheers,
Signature

Joerg Jooss
news-reply@joergjooss.de

davidjgonzalez@gmail.com - 06 Mar 2006 14:23 GMT
oop - thanks for the catch ..

string decodedString = encoding.GetString(characters);
is supposed to read
string decodedString = encoding.GetString(data);

your ReadXml(...) solution from the input stream is just what i needed!
thanks

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.