charset encoding going from std::string to System::String and viceversa [modified]
-
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
-
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
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.
-
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.
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
-
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
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
-
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
System::String
is Unicode compatible. You don't need to do any encoding here. Also I think yourdatabaseManagedWrapper::unMarshalStringUTF8
function's implementation is wrong. Here is a version that converts string contains Unicode characters toSystem::String
.std::wstring
is used instead ofstd::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
-
System::String
is Unicode compatible. You don't need to do any encoding here. Also I think yourdatabaseManagedWrapper::unMarshalStringUTF8
function's implementation is wrong. Here is a version that converts string contains Unicode characters toSystem::String
.std::wstring
is used instead ofstd::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
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
-
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
Well, its easy to convert my code to support
std::string
. You needchar*
instead ifwchar_t*
. Windows useswchar_t
for Unicode and it is 2 bytes wide with UTF-16 encoding. So when programming on windows, it is a better to usewchar_t
.wstring
is atypedef
forbasic_string
templated usingwchar_t
. However, I tried to convert a UTF-8 encoded string to and from managed code. But it never worked until I changed towstring
. Perhaps I may need to investigate more. :)Best wishes, Navaneeth