如何让我的 `std::string url_encode_wstring(const std::wstring &input)` 在 Linux 上工作?

发布于 2024-12-02 01:03:50 字数 933 浏览 2 评论 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下工作?

So we have such function:

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;
     }

Its grate for windows but I wonder how (and is it possible) to make it work under linux?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风和你 2024-12-09 01:03:50

恕我直言,您应该使用便携式字符编解码器库。
这是使用 iconv 的最小可移植代码的示例,这应该足够了。
它应该可以在 Windows 上运行(如果可以,您可以完全删除特定于 Windows 的代码)。
我遵循 GNU 指南,不使用 wcstombs 和 wcstombs。 co 函数 ( https://www.gnu.org/s/你好/manual/libc/iconv-Examples.html
根据用例,适当地处理错误......并且为了提高性能,您可以从中创建一个类。

#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;
}

IMHO you should use a portable character codec library.
Here's an example of minimal portable code using iconv, which should be more than enough.
It's supposed to work on Windows (if it does, you can get rid of your windows-specific code altogether).
I follow the GNU guidelines not to use the wcstombs & co functions ( https://www.gnu.org/s/hello/manual/libc/iconv-Examples.html )
Depending on the use case, handle errors appropriately... and to enhance performance, you can create a class out of it.

#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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文