Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. charset encoding going from std::string to System::String and viceversa [modified]

charset encoding going from std::string to System::String and viceversa [modified]

Scheduled Pinned Locked Moved Managed C++/CLI
databasec++helpquestionannouncement
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andreoli Carlo
    wrote on last edited by
    #1

    Problem on charset encoding obj_db->query("UPDATE PATIENTS_DATA SET FIRST_NAME='flkسؤ' WHERE COD_PATIENT=2 "); give me a warning: 1>.\globalConfiguration.cpp(151) : warning C4566: character represented by universal-character-name '\u0633' cannot be represented in the current code page (1252) 1>.\globalConfiguration.cpp(151) : warning C4566: character represented by universal-character-name '\u0624' cannot be represented in the current code page (1252) and the real string in database is flk?? ----------edit----------- here i didn't pointed out the real problem...see my second post

    modified on Thursday, October 15, 2009 6:14 AM

    L 1 Reply Last reply
    0
    • A Andreoli Carlo

      Problem on charset encoding obj_db->query("UPDATE PATIENTS_DATA SET FIRST_NAME='flkسؤ' WHERE COD_PATIENT=2 "); give me a warning: 1>.\globalConfiguration.cpp(151) : warning C4566: character represented by universal-character-name '\u0633' cannot be represented in the current code page (1252) 1>.\globalConfiguration.cpp(151) : warning C4566: character represented by universal-character-name '\u0624' cannot be represented in the current code page (1252) and the real string in database is flk?? ----------edit----------- here i didn't pointed out the real problem...see my second post

      modified on Thursday, October 15, 2009 6:14 AM

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      barbetto80 wrote:

      obj_db->query("UPDATE PATIENTS_DATA SET FIRST_NAME='flkسؤ' WHERE COD_PATIENT=2 ");

      You have two Arabic characters at the end of the name, and the compiler is merely warning that the current code page does not support correct representation of them.

      A 1 Reply Last reply
      0
      • L Lost User

        barbetto80 wrote:

        obj_db->query("UPDATE PATIENTS_DATA SET FIRST_NAME='flkسؤ' WHERE COD_PATIENT=2 ");

        You have two Arabic characters at the end of the name, and the compiler is merely warning that the current code page does not support correct representation of them.

        A Offline
        A Offline
        Andreoli Carlo
        wrote on last edited by
        #3

        Ok now i pointed out exactly which is my problem :doh: ant it's a charset encoding going from std::string to System::String and viceversa: i query my database and i get the following string tmp_result_string= // in hexadecimal its value is C2 A3 then i convert using: String ^LAST_NAME=tmp_result_string.c_str(); when i set last_name_textBox->Text=LAST_NAME; the visual representation i get in the form is £ which is the ANSII encoding of C2 A3, but what i want is the textbox to display £ which is the utf8 encoding of C2 A3...any help? tha same problem i have doing the inverted thing getting text from textbox and putting it on database.

        modified on Thursday, October 15, 2009 9:22 AM

        A 1 Reply Last reply
        0
        • A Andreoli Carlo

          Ok now i pointed out exactly which is my problem :doh: ant it's a charset encoding going from std::string to System::String and viceversa: i query my database and i get the following string tmp_result_string= // in hexadecimal its value is C2 A3 then i convert using: String ^LAST_NAME=tmp_result_string.c_str(); when i set last_name_textBox->Text=LAST_NAME; the visual representation i get in the form is £ which is the ANSII encoding of C2 A3, but what i want is the textbox to display £ which is the utf8 encoding of C2 A3...any help? tha same problem i have doing the inverted thing getting text from textbox and putting it on database.

          modified on Thursday, October 15, 2009 9:22 AM

          A Offline
          A Offline
          Andreoli Carlo
          wrote on last edited by
          #4

          ok..i find answer to the problem: String^ databaseManagedWrapper::unMarshalStringUTF8(string %os) { char const* buffer = os.c_str(); cli::array<System::Byte>^ a = gcnew cli::array<System::Byte>(os.length()); int i = os.length(); while (i-- > 0) { a[i] = buffer[i]; } return System::Text::Encoding::UTF8->GetString(a); } now i need the inverse... string databaseManagedWrapper::MarshalStringUTF8( String ^ s) { System::Text::UTF8Encoding^ utf8 = gcnew System::Text::UTF8Encoding; array<Byte>^encodedBytes = utf8->GetBytes(s); string os=""; for (int i=0;i<encodedBytes->Length;i++) { os+=encodedBytes[i]; } return os; }

          modified on Thursday, October 15, 2009 9:19 AM

          N 1 Reply Last reply
          0
          • A Andreoli Carlo

            ok..i find answer to the problem: String^ databaseManagedWrapper::unMarshalStringUTF8(string %os) { char const* buffer = os.c_str(); cli::array<System::Byte>^ a = gcnew cli::array<System::Byte>(os.length()); int i = os.length(); while (i-- > 0) { a[i] = buffer[i]; } return System::Text::Encoding::UTF8->GetString(a); } now i need the inverse... string databaseManagedWrapper::MarshalStringUTF8( String ^ s) { System::Text::UTF8Encoding^ utf8 = gcnew System::Text::UTF8Encoding; array<Byte>^encodedBytes = utf8->GetBytes(s); string os=""; for (int i=0;i<encodedBytes->Length;i++) { os+=encodedBytes[i]; } return os; }

            modified on Thursday, October 15, 2009 9:19 AM

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            System::String is Unicode compatible. You don't need to do any encoding here. Also I think your databaseManagedWrapper::unMarshalStringUTF8 function's implementation is wrong. Here is a version that converts string contains Unicode characters to System::String. std::wstring is used instead of std::string.

            String^ UnicodeNativeStringToManagedString(wstring& os)
            {
            const wchar_t* buffer = os.c_str();
            return gcnew String(buffer);
            }

            Here is the function for inverse. You need to work with methods in the Marshal class.

            std::wstring UnicodeManagedStringToNative(String^ str)
            {
            using namespace System::Runtime::InteropServices;
            wchar_t* chr = (wchar_t*) Marshal::StringToHGlobalAnsi(str).ToPointer();
            std::wstring result(chr);
            Marshal::FreeHGlobal(IntPtr(chr));
            return result;
            }

            :)

            Best wishes, Navaneeth

            A 1 Reply Last reply
            0
            • N N a v a n e e t h

              System::String is Unicode compatible. You don't need to do any encoding here. Also I think your databaseManagedWrapper::unMarshalStringUTF8 function's implementation is wrong. Here is a version that converts string contains Unicode characters to System::String. std::wstring is used instead of std::string.

              String^ UnicodeNativeStringToManagedString(wstring& os)
              {
              const wchar_t* buffer = os.c_str();
              return gcnew String(buffer);
              }

              Here is the function for inverse. You need to work with methods in the Marshal class.

              std::wstring UnicodeManagedStringToNative(String^ str)
              {
              using namespace System::Runtime::InteropServices;
              wchar_t* chr = (wchar_t*) Marshal::StringToHGlobalAnsi(str).ToPointer();
              std::wstring result(chr);
              Marshal::FreeHGlobal(IntPtr(chr));
              return result;
              }

              :)

              Best wishes, Navaneeth

              A Offline
              A Offline
              Andreoli Carlo
              wrote on last edited by
              #6

              preamble: i'am using a database that can use UTF8 as best support for multi-language, so i started out from it. UTF8 is a variable-length character encoding for Unicode. The wrapper for the database id written in c++ and uses std::string as variables input method. Well my problem is that i need a std::string to store to the DB, and not wstring... that's why i create these methods. Maybe i didn't get your help as i see you are creating a wstring... ps thank you i really appreciate your answer to my various problem in this forum. You are helping me growing up learning and skilling this language

              N 1 Reply Last reply
              0
              • A Andreoli Carlo

                preamble: i'am using a database that can use UTF8 as best support for multi-language, so i started out from it. UTF8 is a variable-length character encoding for Unicode. The wrapper for the database id written in c++ and uses std::string as variables input method. Well my problem is that i need a std::string to store to the DB, and not wstring... that's why i create these methods. Maybe i didn't get your help as i see you are creating a wstring... ps thank you i really appreciate your answer to my various problem in this forum. You are helping me growing up learning and skilling this language

                N Offline
                N Offline
                N a v a n e e t h
                wrote on last edited by
                #7

                Well, its easy to convert my code to support std::string. You need char* instead if wchar_t*. Windows uses wchar_t for Unicode and it is 2 bytes wide with UTF-16 encoding. So when programming on windows, it is a better to use wchar_t. wstring is a typedef for basic_string templated using wchar_t. However, I tried to convert a UTF-8 encoded string to and from managed code. But it never worked until I changed to wstring. Perhaps I may need to investigate more. :)

                Best wishes, Navaneeth

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups