在C ++中处理汉字字符 [英] Dealing with kanji characters in C++
问题描述
我有一个用C ++编写的Windows桌面应用程序(名为:时间戳),该应用程序使用.NET称为CLR.
I have a windows deskop application (named: Timestamp) written in C++ that use .NET called CLR.
我也有用本机c ++编写的DLL项目(名为:Amscpprest),并使用CPPREST SDK从服务器获取json数据并将数据传递给我的Timestamp应用.
I also have DLL project (named: Amscpprest) written in native c++ and uses CPPREST SDK to get json data from server and pass the data to my Timestamp app.
这是场景: 这是从我的服务器返回的json数据,它是工作人员姓名的列表,并且大多数是用汉字字符写的日语名字.
Here's the scenario: This is the return json data from my server, its a list of staff name and most of it is japanese names written in Kanji characters.
[
{
"staff": {
"id": 121,
"name": "福士 達哉",
"department": [
{
"_id": 3,
"name": "事業推進本部"
}
]
}
},
{
"staff": {
"id": 12,
"name": "北島 美奈",
"department": [
{
"_id": 4,
"name": "事業開発本部"
}
]
}
},
{
"staff": {
"id": 151,
"name": "大河原 紗希",
"department": [
{
"_id": 3,
"name": "事業推進本部"
}
]
}
}
]
这是我的DLL项目(Amscpprest)中的代码.这是获取数据并将其传递到我的CLR项目的方法:
This my code in my DLL project (Amscpprest). This is how get the data and pass to my CLR project:
std::map<int, std::string> staffMap;
auto GetStaffMap() -> std::map<int, std::string> {
return staffMap;
}
void display_json(json::value const & jvalue, utility::string_t const & prefix)
{
try {
//==== Iterate through json data and make an associative array ====/
auto DataArray = jvalue.as_array();
// loop through 'data' object
for (int i = 0; i < DataArray.size(); i++)
{
try
{
auto data = DataArray[i];
auto dataObj = data.at(U("staff")).as_object();
int key;
std::string value;
// loop through each object of 'data'
for (auto iterInner = dataObj.cbegin(); iterInner != dataObj.cend(); ++iterInner)
{
auto &propertyName = iterInner->first;
auto &propertyValue = iterInner->second;
if (propertyName == L"_id")
{
key = propertyValue.as_integer();
}
else if (propertyName == L"name")
{
value = conversions::to_utf8string(propertyValue.as_string());
}
}
staffMap.insert(std::make_pair(key, value));
}
catch (const std::exception& e)
{
std::wcout << e.what() << std::endl;
}
}
}
catch (const std::exception& e) {
std::wcout << e.what() << std::endl;
}
}
pplx::task<http_response> task_request(http_client & client, method mtd, json::value const & jvalue, std::string searchText)
{
//std::string url = "/api/authenticate/searchStaffs/";
std::string url = "/api/authenticate/oldgms/staffs_id_name/";
return client.request(mtd, utility::conversions::to_string_t(url));
}
void make_request(http_client & client, method mtd, json::value const & jvalue, std::string searchText)
{
task_request(client, mtd, jvalue, searchText)
.then([](http_response response)
{
if (response.status_code() == status_codes::OK)
{
return response.extract_json();
}
return pplx::task_from_result(json::value());
})
.then([](pplx::task<json::value> previousTask)
{
try
{
display_json(previousTask.get(), L"R: ");
}
catch (http_exception const & e)
{
std::wcout << e.what() << std::endl;
}
})
.wait();
}
int SearchStaff(std::string searchText)
{
//clear staffMap every call
staffMap.clear();
http_client client(U("http://52.68.13.154:3000"));
auto nullValue = json::value::null();
//std::string search_text = conversions::to_utf8string(L"北島 美奈");
make_request(client, methods::GET, nullValue, searchText);
return staff_id;
}
这是我在CLR项目(时间戳)中的代码.这就是我从dll项目接受数据并显示到用户界面的方式.
And this is my code in my CLR project (Timestamp). This is how i accept the data from my dll project and display to the user interface.
String^ input = searchBox->Text;
std::string searchText = msclr::interop::marshal_as<std::string>(input);
// Clear listView item every type in searchbox
listView1->Items->Clear();
Staffs::SearchStaff(searchText);
std::map<int, std::string> staffMap = Staffs::GetStaffMap();
std::map<int, std::string>::iterator iter;
for (iter = staffMap.begin(); iter != staffMap.end(); iter++)
{
String^ value = msclr::interop::marshal_as<System::String^>(iter->second);
int key = iter->first;
listViewItem = gcnew Windows::Forms::ListViewItem(value);
listViewItem->SubItems->Add(System::Convert::ToString(key));
this->listView1->Items->Add(this->listViewItem);
}
我希望它应该在列表视图中正确显示名称和ID,但这是结果:
I expect that it should display the names and id correctly in the listview but this was the result:
我希望有人可以帮助我解决这个问题.
I'm hoping that someone could help me solve this problem.
推荐答案
我认为您在这里有两个不同的问题.
I think you have two distinct issues here.
首先,在迭代中,您尝试读取一个名为_id
的键,该键不存在(应该为id
),因此您的int key
永远不会被赋值(并且不会被初始化,这就是为什么您在列表视图中得到一个奇怪的数字的原因.
First, in the iteration, you try to read a key called _id
, which isn't there (it should be id
), so your int key
is never assigned a value (and it's not initialized, that's why you get a weird number in your list view).
第二,您必须将utf8(存储在std::string
中)转换为ucs2,这是.NET字符串的组成部分.您可以使用UTF8Encoding
类
Second, you have to convert utf8 (stored in std::string
) to ucs2, which is what a .NET String is made of. You can achieve that using the UTF8Encoding
class (docs here) . So, instead of this:
String^ value = msclr::interop::marshal_as<System::String^>(iter->second);
您需要这样的东西:
//make a byte array to hold the string chars
array<Byte>^ bytes = gcnew array<Byte>(iter->second.size());
//copy the string chars into the byte array
System::Runtime::InteropServices::Marshal::Copy(IntPtr(&iter->second[0]), bytes, 0, iter->second.size());
//get a string from the bytes, using UTF8Encoding
String^ value = System::Text::UTF8Encoding::UTF8->GetString(bytes);
这篇关于在C ++中处理汉字字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!