惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
S
Securelist
D
Docker
The Register - Security
The Register - Security
GbyAI
GbyAI
Recorded Future
Recorded Future
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
罗磊的独立博客
博客园 - 【当耐特】
F
Full Disclosure
WordPress大学
WordPress大学
腾讯CDC
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
SecWiki News
SecWiki News
L
Lohrmann on Cybersecurity
I
InfoQ
MyScale Blog
MyScale Blog
量子位
Cyberwarzone
Cyberwarzone
博客园 - 三生石上(FineUI控件)
The Hacker News
The Hacker News
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
博客园_首页
H
Help Net Security
K
Kaspersky official blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Webroot Blog
Webroot Blog
Blog — PlanetScale
Blog — PlanetScale
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
The Cloudflare Blog
P
Proofpoint News Feed
V
Visual Studio Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
爱范儿
爱范儿
P
Privacy International News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
B
Blog RSS Feed

博客园 - 小水坝

使用双分派解决领域实体和外部机制通信问题 搞定thrift双向消息 读《软件需求最佳实践》有感 __declspec(dllimport)的小秘密(转) 跨线程send message 【boost】使用serialization库序列化子类 【boost】ptree 读写中文的问题 动态创建TeeChart的简便方法 【MFC】动态创建CMFCToolbar图标不显示问题 【boost】使用装饰者模式改造boost::thread_group 【VC】VC工具栏图标合并工具(非tbcreator和visual toolbar) 【boost】使用lambda表达式和generate_n生成顺序序列 【boost】BOOST_LOCAL_FUNCTION体验 【boost】MFC dll中使用boost thread的问题 【转帖】C++编译原理 资料 IE6,7下password框长度变短问题 dwz局部表格分页 dwz中combox的value问题 dwz中使用flot,js报表等js插件
【thrift】vc中使用thrift中文字符串乱码问题解决
小水坝 · 2014-10-25 · via 博客园 - 小水坝

问题描述:

VC中使用Apache thrift时,如果字符串中包含中文,会出现乱码问题,这个问题的原因是由于thrift为了达到跨语言交互而使用了UTF-8格式发送字符串,这点对java或者C#不会造成影响,但是在VC中UTF-8却很成问题。VC中的string编码随项目编码一般是multibytes或者unicode,虽然倡导使用unicode,但实际上使用multibytes多字节开发仍然广泛存在,下面的解决方案主要解决的是多字节下的乱码问题。

解决方案

1、手动转换

第一种解决方案就是在使用的时候,自己手动转换,读取时从utf-8转为multibytes,写入时从multibytes转为utf-8。显然这样费时费力,只适用于中文字符存在较少的场景。

2、修改thrift lib库

为了达到一劳永逸的目的,可以修改thrift c++ lib库来完成转换,这里只分析使用TBinaryProtocol的场景,其他Protocol如果出现相同情况请参照。

打开TBinaryProtocol.h和TBinaryProtocol.tcc,修改其readString和writeString方法

template <class Transport_>
template<typename StrType>
uint32_t TBinaryProtocolT<Transport_>::readString(StrType& str) {
  uint32_t result;
  int32_t size;
  result = readI32(size);
  result += readStringBody(str, size);
 //modified by xiaosuiba
  //convert utf-8 to multibytes
#ifdef _WIN32
    str = utf8_to_mb(str);
#endif
  return result;
}
template <class Transport_>
template<typename StrType>
uint32_t TBinaryProtocolT<Transport_>::writeString(const StrType& str) {
    //modified by xiaosuiba
    //添加多字节到UTF-8转换
    
#ifdef _WIN32
    StrType theStr = mb_to_utf8(str);
#else
    const StrType &theStr = str;
#endif

  if(theStr.size() > static_cast<size_t>((std::numeric_limits<int32_t>::max)()))
    throw TProtocolException(TProtocolException::SIZE_LIMIT);
  uint32_t size = static_cast<uint32_t>(theStr.size());
  uint32_t result = writeI32((int32_t)size);
  if (size > 0) {
    this->trans_->write((uint8_t*)theStr.data(), size);
  }
  return result + size;
}

 重新编译lib库,测试OK。

这样会存在一定的效率损失(读取写入都会复制一遍),但是相对于手动转换却能大大节省工作量。

其中的转换函数mb_to_utf8和utf8_to_mb可以在网上找到大量源码。