473,320 Members | 2,027 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,320 software developers and data experts.

Convert string to utf-8

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
3 52352

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nathan | last post by:
Is there a way to convert a string to a CipherMessage? I am calling a function that decrypts a CipherMessage and returns the value. The only problem is when I want to use an encrypted value stored...
4
by: Albert Jan | last post by:
Hi, I have text from mime email messages with different encoding that I want to convert to utf-8, but I'm relatively new on encoding problems. I use the following code, but this doesn't seem...
3
by: Petr Jakes | last post by:
Hi, I am trying to convert string to the "escaped string". example: from "0xf" I need "\0xf" I am able to do it like: a="0xf" escaped_a=("\%s" % a ).decode("string_escape") But it looks a...
5
by: XML newbie: Urgent pls help! | last post by:
function to convert string to 1 dimensional array of long in VB.Net
3
by: Ursula | last post by:
Is it possible to convert a string in a file. The problem is this: I have an object string that is a file xml and I want to pass to Deserialize function, but Deserialize function expect an object...
3
by: Shawn Ferguson | last post by:
Hello All, I'm trying to do what I would think would be simple and straightforward, but it is not. I have a 2 textbox on a form, a label, and a button, when I click the botton I want to add the...
4
by: Man4ish | last post by:
HI , I am trying to convert string into char array of characters.but facing problem. #include <iostream> #include <string> using namespace std; int main() { string t="1,2,3,4,5,6";
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
9
by: engteng | last post by:
How do I convert string to numeric in VB.NET 2003 ? Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle then not convert. Regards, Tee
4
by: =?ISO-8859-2?Q?Boris_Du=B9ek?= | last post by:
Hi, I have an API that returns UTF-8 encoded strings. I have a utf8 codevt facet available to do the conversion from UTF-8 to wchar_t encoding defined by the platform. I have no trouble...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.