By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,611 Members | 1,642 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,611 IT Pros & Developers. It's quick & easy.

Convert string to utf-8

P: n/a
Hi

I have a string, which I must convert into another string and have it
utf-8 encoded.

My current code looks like this;

Both vDataIn and vDataOut are Objects (parameters to a function-call),
and vDataIn contains a string, and that is also what vDataOut must
contain when I am done.

vDataOut = _
System.Text.Encoding.UTF8.GetString( _
System.Text.Encoding.Convert(System.Text.Encoding. UTF8, _
System.Text.Encoding.UTF8, _
System.Text.Encoding.UTF8.GetBytes(vDataIn)))

But is is pretty ugly, if you ask me. Is there a more pretty way?

--
Jan Eliasen, representing himself and not the company he works for.
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

http://www.planet-source-code.com/vb...1088&lngWId=10

Jan Eliasen wrote:
Hi

I have a string, which I must convert into another string and have it
utf-8 encoded.

My current code looks like this;

Both vDataIn and vDataOut are Objects (parameters to a function-call),
and vDataIn contains a string, and that is also what vDataOut must
contain when I am done.

vDataOut = _
System.Text.Encoding.UTF8.GetString( _
System.Text.Encoding.Convert(System.Text.Encoding. UTF8, _
System.Text.Encoding.UTF8, _
System.Text.Encoding.UTF8.GetBytes(vDataIn)))

But is is pretty ugly, if you ask me. Is there a more pretty way?

Nov 20 '05 #2

P: n/a
Cor
Hi Jan,

Till Jay B is not here (he lives I thought on the other side of the ditch
for us) you can search for the answer Jay Be has given in this newsgroup
about this.

But you go very far in his kind of solutions so I think he maybe will give
you the right answer.

Cor

Nov 20 '05 #3

P: n/a
Hi Jan,

I agree with you that there should be an overload of Convert which
takes a String and returns a String. A lot of people have queries about
encoding strings.

This is a bit neater, perhaps.

Dim utf8 As Encoding = System.Text.Encoding.UTF8
vDataOut = utf8.GetString (utf8.Convert (utf8, utf8,
utf8.GetBytes(vDataIn)))

However, both versions turn the vDataIn string into a UTF8 byte array
which is converted to a different copy od the same UTF8 byte array which
is then converted to a UTF string.

Shouldn't there be another encoding in there somewhere??

Dim utfS As Encoding = System.Text.Encoding.???? 'Src
Dim utfD As Encoding = System.Text.Encoding.UTF8 'Dest
vDataOut = utfD.GetString (utfD.Convert (utfS, utfD,
utfS.GetBytes(vDataIn)))

In both cases the utfX used with Convert doesn't matter as it's just
shorthand for the class - utfX.Convert = System.Text.Encoding.Convert.

Regards,
Fergus
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.