当前位置: 首页 > news >正文

关于Protobuf 输入输出中文到文件中的一系列问题

一、不含中文的常规处理

        

//定义
message Value
{repeated uint32 uiMain = 1; repeated uint32 uiSub  = 2;  
}message Simulate
{repeated Value data     = 1;
}//文件
data
{uiMainAds :		36598uiMainAds :		35675uiMainAds :		36756  uiSubAds :		16924uiSubAds :		16488uiSubAds :		16984  
}
data
{uiMainAds :		36598uiMainAds :		35675uiMainAds :		36756  uiSubAds :		16924uiSubAds :		16488uiSubAds :		16984  
}

        1、从文件中读取 

    //适用于linux 平台,因为可以方便的获取文件描述符int infile = open("./sys.cfg", O_RDONLY);if (infile < 0){printf("open sys.cfg failed.\n");return;}google::protobuf::io::FileInputStream fileInput(infile);if (!google::protobuf::TextFormat::Parse(&fileInput, &m_cfg)) //将文件内容转为m_cfg(SysConfig){printf("Parse sys.cfg failed.\n");close(infile);return;}close(infile);//c++ 通用模式std::ifstream input("./cfg/debugger.cfg", std::ios::in );if (!input){return false;}DebugFile m_cfgFileData;google::protobuf::io::IstreamInputStream istr( &input);bool pa = google::protobuf::TextFormat::Parse(&istr, &m_cfgFileData);

        2、写入文件(是人可读的,有点类似与json 格式那种)

    //c++ 通用模式 std::ofstream ofs("./cfg/test.txt", std::ios_base::out | std::ios_base::binary);if (!ofs){return false;}DebugFile m_cfgFileData; google::protobuf::io::OstreamOutputStream ostr( &ofs);google::protobuf::TextFormat::Print(m_cfgFileData, &ostr);

   二、如果定义中是bytes ,且值是中文的

        

//定义
message STParaInfo
{required bytes strParaName = 1; //参数名称required ParaType paraType	= 2; //参数类型	required uint32 uiValue = 3; //参数值	
};
message STSendPara
{repeated STParaInfo stParaInfos = 1; //参数信息
};//文件
stSendPara{stParaInfos{strParaName : "子功能2参数1"paraType : ParaType_OneuiValue : 1}stParaInfos{strParaName : "子功能2参数2"paraType : ParaType_TwouiValue : 1}stParaInfos{strParaName : "子功能2参数3"paraType : ParaType_FouruiValue : 1}}

        1、读取,还是可以正常读取,但是获取到的 strParaName 值,是八进制转义字符串,,如果是qt 等使用,使用QString 去转一下,还是可以直接获取到中文字符串的,

        2、写入,,如果直接使用上述方式写入,,,文件中的中文 就是八进制转义字符串,,是人不可读的,,如下

        strParaName : "\346\265\213\350\257\225\345\215\225\345\205\203 "

   三、解决,只能通过自己转换的方式

        

//ai 编写的转换函数
std::string octalToChinese(const std::string& input) {std::string result;size_t i = 0;while (i < input.length()) {if (input[i] == '\\' && i + 3 < input.length() &&input[i + 1] >= '0' && input[i + 1] <= '7' &&input[i + 2] >= '0' && input[i + 2] <= '7' &&input[i + 3] >= '0' && input[i + 3] <= '7') {// Extract the octal sequencestd::string octal = input.substr(i + 1, 3);// Convert octal string to integerint octalValue = std::stoi(octal, nullptr, 8);// Convert integer value to corresponding characterresult += static_cast<char>(octalValue);// Move to the next character after the octal sequencei += 4;} else {// Append regular character to resultresult += input[i];i++;}}return result;
}

        

    //使用std::ofstream ofs("./cfg/test.txt", std::ios_base::out | std::ios_base::binary);if (!ofs){return false;}//这里用Utf8DebugString 去获取proto mssage 的 字符串 ,然后使用转换函数转换 ,不用序列化是因为序列化的字符串是不可读的std::string fout = octalToChinese(m_cfgFileData.Utf8DebugString());ofs << fout;

http://www.lryc.cn/news/409273.html

相关文章:

  • 后端笔记(1)--javaweb简介
  • 便携式气象监测系统的优势:精准高效,随行监测
  • uniapp App判断是否安装某个app
  • C/C++大雪纷飞代码
  • 【linux】【设备树】具有 GPIO 控制器和连接器的硬件配置的备树(Device Tree)代码讲解
  • 【2025留学】德国留学真的很难毕业吗?为什么大家不来德国留学?
  • Apache Solr 最常用的命令
  • 经济下行,企业还在“裁员至上”?
  • 学习笔记之Java篇(0729)
  • 吃肉的刷题记录4-基础知识-字符串
  • 人工智能与机器学习原理精解【7】
  • ResNet学习笔记
  • 使用chainlit快速构建类似OPEN AI一样的对话网页
  • 【根据字符出现频率排序】python刷题记录
  • 活动报名小程序
  • unity基础问题
  • RedHat Enterprise Linux 7 YUM源(本地/网络源)配置详解
  • 关于顺序表数组下标的一些关系梳理
  • VS C++ Project(项目)的工作目录设置
  • STM32自定义协议串口接收解析指令程序
  • STM32——GPIO(点亮LEDLED闪烁)
  • VulnHub靶机入门篇--kioptrix.level 3
  • aiGPT系统源码★重大升级★AI写作/AI绘画/AI音乐/AI视频
  • Vue Router高级用法:动态路由与导航守卫
  • 江科大/江协科技 STM32学习笔记P9-11
  • 【培训通知】成为Power BI数据分析可视化实战第一人,加入3天直播即可
  • 24暑假算法刷题 | Day22 | LeetCode 77. 组合,216. 组合总和 III,17. 电话号码的字母组合
  • 一篇文章告诉你对讲机为什么不能被手机取代的7个原因
  • LION论文阅读
  • 在Android上实现汉字笔顺动画效果——HanZiWriter