Ajax请求发送的UTF8编码字符串传到后台使用std:string进一步处理,如果包含中文会出现中文乱码的问题:

   

  特找了一下转码的解决方法,直接代码如下:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 
// UTF8转std:string
// 转换过程:先将utf8转双字节Unicode编码,再通过WideCharToMultiByte将宽字符转换为多字节。
std::string UTF8_To_string(const std::string& str) 

    ); 
    ];    //一定要加1,不然会出现尾巴 
); 
    MultiByteToWideChar(CP_UTF8, , str.c_str(), str.length(), pwBuf, nwLen); 
    , NULL, NULL, NULL, NULL); 
    ]; 
    memset(pBuf, ); 
    WideCharToMultiByte(CP_ACP, , pwBuf, nwLen, pBuf, nLen, NULL, NULL);

std::string strRet = pBuf;

delete []pBuf; 
    delete []pwBuf; 
    pBuf = NULL; 
    pwBuf = NULL;

return strRet; 
}

// std:string转UTF8
std::string string_To_UTF8(const std::string& str) 

    ); 
    ];    //一定要加1,不然会出现尾巴 
); 
    ::MultiByteToWideChar(CP_ACP, , str.c_str(), str.length(), pwBuf, nwLen); 
    , NULL, NULL, NULL, NULL); 
    ]; 
    ZeroMemory(pBuf, nLen + ); 
    ::WideCharToMultiByte(CP_UTF8, , pwBuf, nwLen, pBuf, nLen, NULL, NULL);

std::string strRet(pBuf);

delete []pwBuf; 
    delete []pBuf; 
    pwBuf = NULL; 
    pBuf  = NULL;

return strRet; 

问题解决bingo!

UTF8与std:string互转的更多相关文章

  1. 构造UTF8的std::string

    在VC++的世界里,MS比较鼓励使用_UNICODE,std::wstring.而在Web, XML则提倡用UTF8.当在C++的程序里要保存/读取XML数据,就存在wstring与string之间的 ...

  2. QString 和std::string互转

    std::string cstr; QString qstring; //****从std::string 到QString qstring = QString(QString::fromLocal8 ...

  3. ue4 fstring 和std::string互转

    https://forums.unrealengine.com/development-discussion/c-gameplay-programming/6517-convert-std-strin ...

  4. VC++ 中使用 std::string 转换字符串编码

    目录 第1章说明    1 1.1 代码    1 1.2 使用    4 第1章说明 VC++中宽窄字符串的相互转换比较麻烦,借助std::string能大大减少代码量. 1.1 代码 函数声明如下 ...

  5. 计算std:string的字节长度

    如果项目本身是使用 Unicode 字符集和utf8编码,std::string的length(),size()甚至是c的strLen取到的都是字节长度了,比如三个汉字,就是9, 以上情况不满足的话, ...

  6. PHP UTF-8和Unicode编号互转

    PHP UTF-8和Unicode编号互转 /** * utf-8 转unicode * * @param string $name * @return string */ function utf8 ...

  7. 如何使用 window api 转换字符集?(std::string与std::wstring的相互转换)

    //宽字符转多字节 std::string W2A(const std::wstring& utf8) { int buffSize = WideCharToMultiByte(CP_ACP, ...

  8. java byte数组与String互转

      java byte数组与String互转 CreationTime--2018年7月6日14点53分 Author:Marydon 1.String-->byte[] 方法:使用String ...

  9. std::u32string conversion to/from std::string and std::u16string

    I need to convert between UTF-8, UTF-16 and UTF-32 for different API's/modules and since I know have ...

随机推荐

  1. client version is higher than daemon version (client is v.1.29 daemon is v.1.22)

    安装好coreseek,建了索引,启动了服务,用php建了一个test.php,用于测试:<?phpinclude_once('sphinxapi.php');//向搜索引擎发起请求 $cl = ...

  2. THEOS的第一个TWeak的成功创建

    THEOS的第一个TWeak的成功创建html, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMi ...

  3. am335x SPI spi_d0, spi_d1 out, in 模式设定

    AM335x SPI DO DI 的模式 参考: https://www.deyisupport.com/question_answer/dsp_arm/sitara_arm/f/25/t/13826 ...

  4. PostgreSQL学习手册(数据表)<转>

    一.表的定义: 对于任何一种关系型数据库而言,表都是数据存储的最核心.最基础的对象单元.现在就让我们从这里起步吧.    1. 创建表:    CREATE TABLE products (      ...

  5. 隐藏的Word快捷键操作

    原文地址:http://tieba.baidu.com/p/4163778583 原文来自于: 新文咖从会用电脑开始,Microsoft Office Word就是我们最常用的软件.靠着它,我们写论文 ...

  6. mysql找到所有索引

    SELECT a.TABLE_SCHEMA,a.TABLE_NAME,a.index_name,GROUP_CONCAT(column_name ORDER BY seq_in_index) AS ` ...

  7. 利用SpringCloud搭建一个最简单的微服务框架

    http://blog.csdn.net/caicongyang/article/details/52974406 1.微服务 微服务主要包含服务注册,服务发现,服务路由,服务配置,服务熔断,服务降级 ...

  8. MFC函数——CWnd::OnEraseBkgnd

    CWnd::OnEraseBkgnd afx_msg BOOL OnEraseBkgnd( CDC* pDC ); 返回值: 如果它擦除了背景,则返回非零值:否则返回0. 参数: pDC 指定了设备环 ...

  9. nvalid bound statement (not found)

    问题详情: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.coocaa.te ...

  10. php 二维数组打乱顺序

    public function shuffle_assoc($list) { if (!is_array($list)) return $list; $keys = array_keys($list) ...