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 / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

UTF-8 std::string to System::String^

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
barnum@bluezone.no - 19 Oct 2007 19:43 GMT
Hi,

I have a std::string which I know is UTF-8 encoded. How can I make a
System::String^ from it?
I tried UTF8Encoding class, but it wants a Byte array, and I don't
know how to get that from a std::string.

Thanks for any help!
jehugaleahsa@gmail.com - 19 Oct 2007 20:01 GMT
On Oct 19, 12:43 pm, bar...@bluezone.no wrote:
> Hi,
>
[quoted text clipped - 4 lines]
>
> Thanks for any help!

Since you know that your string is UTF-8, you need only be worried
that chars are 8-bits wide.

std::string s = "Hello, World";
char const* buffer = s.c_str();

>From this point you can fill a Byte array. I am not totally sure how
to do this in Managed C++, but it should get you to where you are
going.

Outside of that I can't help you.
Ben Voigt [C++ MVP] - 19 Oct 2007 21:06 GMT
> On Oct 19, 12:43 pm, bar...@bluezone.no wrote:
>> Hi,
[quoted text clipped - 15 lines]
> to do this in Managed C++, but it should get you to where you are
> going.

C++/CLI, not Managed C++

Add these lines to get a Byte array.

cli::array<System::Byte>^ a = gcnew cli::array<System::Byte>(s.length());
int i = s.length();
while (i-- > 0)
   a[i] = buffer[i];

And then use Encoding::UTF8::GetString(a);

> Outside of that I can't help you.
barnum@bluezone.no - 20 Oct 2007 12:35 GMT
> <jehugalea...@gmail.com> wrote in message
>
[quoted text clipped - 36 lines]
>
> - Vis sitert tekst -

Thanks, that worked!
Giovanni Dicanio - 19 Oct 2007 22:58 GMT
> I have a std::string which I know is UTF-8 encoded. How can I make a
> System::String^ from it?
> I tried UTF8Encoding class, but it wants a Byte array, and I don't
> know how to get that from a std::string.

To add to what others have written, I think that you could also convert the
string from Unicode UTF-8 to Unicode UTF-16 inside C++, using
::MultiByteToWideChar Win32 API.
Then, you can pass the resulting Unicode UTF-16 string value to
System::String (so, in this way you bypass UTF8Encoding class).

Giovanni

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.