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. CSS标签类型和样式表继承与优先级

    标签类型 块级标签 什么是块级标签:在html中<div>. <p>.h1~h6.<form>.<ul> 和 <li>就是块级元素 块级标签 ...

  2. Android keytool 生成keystore与给APK签名

    http://www.tttabc.com/android/keytool-keystore-jarsigner-apk.htm

  3. 机器学习----人脸对齐的算法-ASM.AAM..CLM.SDM

    引自:http://blog.csdn.net/linolzhang/article/details/55271815 人脸检测 早已比较成熟,传统的基于HOG+线性分类器 的方案检测效果已经相当不错 ...

  4. redis连接超时问题排查

    连接池无法获取到连接或获取连接超时redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource f ...

  5. R语言学习笔记 (入门知识)

    R免费使用:统计工具:# 注释,行注释块注释:anything="这是注释的内容"常用R语言编辑器:Rsutdio,Tinn-R,Eclipse+StatET:中文会有乱码帮助:? ...

  6. [Intellij] Project Structure 配置说明

    IntelliJ IDEA 的Project structure可以在File->Project structure中打开,同时,在新建项目是IDE一般用向导的方式让你填写Project str ...

  7. Python 类的多态的运用

    #类的多态的运用 #汽车类 class Car(object): def move(self): print("move ...") #汽车商店类 class CarStore(o ...

  8. HashMap的长度为什么要是2的n次方

    HashMap为了存取高效,要尽量较少碰撞,就是要尽量把数据分配均匀,每个链表长度大致相同,这个实现就在把数据存到哪个链表中的算法: 这个算法实际就是取模,hash%length,计算机中直接求余效率 ...

  9. Java中static、final、static final的区别(转)+transient

    说明:不一定准确,但是最快理解. final: final可以修饰:属性,方法,类,局部变量(方法中的变量) final修饰的属性的初始化可以在编译期,也可以在运行期,初始化后不能被改变. final ...

  10. qualcomm compile instructions

    qualcomm编译指令 Compile the Entire Android Software source build/envsetup.sh lunch msm8909-userdebug ma ...