C++ 如何使我的'std::stringurl_encode_wstring(const std::wstring&;input)`在Linux上工作?
因此,我们需要这样的功能:C++ 如何使我的'std::stringurl_encode_wstring(const std::wstring&;input)`在Linux上工作?,c++,linux,string,urlencode,wstring,C++,Linux,String,Urlencode,Wstring,因此,我们需要这样的功能: std::string url_encode_wstring(const std::wstring &input) { std::string output; int cbNeeded = WideCharToMultiByte(CP_UTF8, 0, input.c_str(), -1, NULL, 0, NULL, NULL); if (cbNeeded > 0) {
std::string url_encode_wstring(const std::wstring &input)
{
std::string output;
int cbNeeded = WideCharToMultiByte(CP_UTF8, 0, input.c_str(), -1, NULL, 0, NULL, NULL);
if (cbNeeded > 0) {
char *utf8 = new char[cbNeeded];
if (WideCharToMultiByte(CP_UTF8, 0, input.c_str(), -1, utf8, cbNeeded, NULL, NULL) != 0) {
for (char *p = utf8; *p; *p++) {
char onehex[5];
_snprintf(onehex, sizeof(onehex), "%%%02.2X", (unsigned char)*p);
output.append(onehex);
}
}
delete[] utf8;
}
return output;
}
它适用于windows,但我想知道如何(以及是否可能)使它在linux下工作?我想你应该如何使用便携式字符编解码器库。 下面是一个使用iconv的最小可移植代码示例,这应该足够了。 它应该可以在Windows上工作(如果可以的话,您可以完全删除Windows特定的代码)。 我遵循GNU指南,不使用wcstombs&co函数() 根据用例,适当地处理错误。。。为了提高性能,您可以从中创建一个类
#include <iostream>
#include <iconv.h>
#include <cerrno>
#include <cstring>
#include <stdexcept>
std::string wstring_to_utf8_string(const std::wstring &input)
{
size_t in_size = input.length() * sizeof(wchar_t);
char * in_buf = (char*)input.data();
size_t buf_size = input.length() * 6; // pessimistic: max UTF-8 char size
char * buf = new char[buf_size];
memset(buf, 0, buf_size);
char * out_buf(buf);
size_t out_size(buf_size);
iconv_t conv_desc = iconv_open("UTF-8", "wchar_t");
if (conv_desc == iconv_t(-1))
throw std::runtime_error(std::string("Could not open iconv: ") + strerror(errno));
size_t iconv_value = iconv(conv_desc, &in_buf, &in_size, &out_buf, &out_size);
if (iconv_value == -1)
throw std::runtime_error(std::string("When converting: ") + strerror(errno));
int ret = iconv_close(conv_desc);
if (ret != 0)
throw std::runtime_error(std::string("Could not close iconv: ") + strerror(errno));
std::string s(buf);
delete [] buf;
return s;
}
int main() {
std::wstring in(L"hello world");
std::wcout << L"input: [" << in << L"]" << std::endl;
std::string out(wstring_to_utf8_string(in));
std::cerr << "output: [" << out << "]" << std::endl;
return 0;
}
#包括
#包括
#包括
#包括
#包括
std::string wstring_到utf8_字符串(const std::wstring&input)
{
size\u t in\u size=input.length()*sizeof(wchar\u t);
char*in_buf=(char*)input.data();
size\u t buf\u size=input.length()*6;//悲观:最大UTF-8字符大小
char*buf=新字符[buf_大小];
memset(buf,0,buf_大小);
字符*out_buf(buf);
尺寸输出尺寸(buf尺寸);
iconv_t conv_desc=iconv_open(“UTF-8”、“wchar_t”);
如果(conv_desc==iconv_t(-1))
抛出std::runtime_错误(std::string(“无法打开iconv:”)+strerror(errno));
大小iconv值=iconv(conv_desc,&in_buf,&in_size,&out_buf,&out_size);
如果(iconv_值==-1)
抛出std::runtime_错误(std::string(“转换时:”)+strerror(errno));
int ret=iconv\U close(转换描述);
如果(ret!=0)
抛出std::runtime_错误(std::string(“无法关闭iconv:”)+strerror(errno));
std::字符串s(buf);
删除[]buf;
返回s;
}
int main(){
std::wstring in(L“hello world”);
std::wcout使用wcstombs
和mbstowcs
。看一看一些示例代码。对于wctombs
的许多反对意见并不适用于std::locale
和co。不过,使用iconv
是一个好建议。