0

I'm trying to convert a string like

<Root>á</Root>

To it's UTF string representation, like this

<Root>á</Root>

(Taken from this page: http://www.cafewebmaster.com/online_tools/utf8_encode)

But when I issue Encoding.UTF8.GetBytes(str) I get an array of utf bytes.

How can I convert those bytes to the string representation I'm after?

--

Thanks for pointing that there is no string representation of an utf8 string.

Just to clarify my needs, I have to execute something like this in sql 2008:

xmlAuditoria_Alta 
'
<Out>utf8 char: á</Out>
'

This is the only way I found so far to have this stored precedure correctly save the value

utf8 char: á

That's why I'm trying to convert from á to á

Perhaps there's a more correct way to do it

3
  • 2
    There is no such thing as a "UTF string representation". á is what happens when you incorrectly parse UTF8 bytes as a single-byte encoding. You should not do that. Commented Jan 10, 2014 at 19:59
  • Please use proper API to work with XML... Commented Jan 10, 2014 at 20:04
  • For what it is worth, taken at face value the answer to your question is: Encoding _1252 = new Encoding(1252); // or whatever single byte encoding you desire byte[] utf8 = Encoding.UTF8.GetBytes(str); string mangledStr = _1252.GetString(utf8); But I am positive that you do not ever want to do this! Commented Jan 10, 2014 at 20:04

1 Answer 1

8

Your question is based on an erroneous premise.

<Root>á</Root>

is not the UTF-8 representation of your string. In fact that string is the UTF-8 bytes re-interpreted in some other single-byte 8 bit character set.

If you want to convert a C# string to UTF-8 then you do indeed write:

Encoding.UTF8.GetBytes(str)
Sign up to request clarification or add additional context in comments.

1 Comment

+1. Also default encoding for XML is UTF-8 already, so there is a good chance that simply saving XML to stream will produce expected result (instead of direct string manipulation).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.