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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • barnum@bluezone.no

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

    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

    #2
    Re: UTF-8 std::string to System::String^

    On Oct 19, 12:43 pm, bar...@bluezone .no wrote:
    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!
    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.

    Comment

    • Ben Voigt [C++ MVP]

      #3
      Re: UTF-8 std::string to System::String^


      <jehugaleahsa@g mail.comwrote in message
      news:1192820470 .187763.240190@ t8g2000prg.goog legroups.com...
      On Oct 19, 12:43 pm, bar...@bluezone .no wrote:
      >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!
      >
      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.
      C++/CLI, not Managed C++

      Add these lines to get a Byte array.

      cli::array<Syst em::Byte>^ a = gcnew cli::array<Syst em::Byte>(s.len gth());
      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.
      >

      Comment

      • Giovanni Dicanio

        #4
        Re: UTF-8 std::string to System::String^


        <barnum@bluezon e.noha scritto nel messaggio
        news:1192819417 .018640.229780@ i13g2000prf.goo glegroups.com.. .
        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
        ::MultiByteToWi deChar 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


        Comment

        • barnum@bluezone.no

          #5
          Re: UTF-8 std::string to System::String^

          On 19 Okt, 22:06, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
          <jehugalea...@g mail.comwrote in message
          >
          news:1192820470 .187763.240190@ t8g2000prg.goog legroups.com...
          >
          >
          >
          >
          >
          On Oct 19, 12:43 pm, bar...@bluezone .no wrote:
          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!
          >
          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.
          >
          C++/CLI, not Managed C++
          >
          Add these lines to get a Byte array.
          >
          cli::array<Syst em::Byte>^ a = gcnew cli::array<Syst em::Byte>(s.len gth());
          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.- Skjul sitert tekst -
          >
          - Vis sitert tekst -- Skjul sitert tekst -
          >
          - Vis sitert tekst -
          Thanks, that worked!

          Comment

          Working...