将unsigned char *或unsigned char []转换为std::string

引言

在C++中,unsigned char *或unsigned char \[\]是无法直接转换为std::string的,比如有下面例子:

cpp 复制代码
#include <iostream>
#include <string.h>
#include <string>

using namespace  std;

int main()
{
    unsigned char cbuffer[]={0x61,0x62,0x63,0x0};
    string sbuffer=cbuffer; // error
    cout << sbuffer << endl;
    return 0;
}

编译,会发现报错:

所以该如何将unsigned char *或unsigned char \[\]转换为std::string呢,有如下方法。

方法一、使用reinterpret_cast

通过reinterpret_cast可以进行强制类型转换:

cpp 复制代码
#include <iostream>
#include <string.h>
#include <string>
 
using namespace  std;


int main()
{
    const unsigned char cbuffer[]={0x61,0x62,0x63,0x0,0x64,0x65};
    string sbuffer=reinterpret_cast<const char*>(cbuffer);

    cout << sbuffer << endl;
    return 0;
}

编译,运行效果如下。可以看到编译没有出错,但是std::string会丢失'\0'之后的数据,出现截断问题:

所以可以使用std::string的构造函数:string(size_type n,char c),用来创建一个包含n个元素的string对象,对象中的每个元素都被初始化为字符c。修改上述例子为:

cpp 复制代码
#include <iostream>
#include <string.h>
#include <string>
 
using namespace  std;


int main()
{
    const unsigned char cbuffer[]={0x61,0x62,0x63,0x0,0x64,0x65};
    string sbuffer( reinterpret_cast<const char *>(cbuffer), sizeof(cbuffer) ) ;

    cout << sbuffer << endl;
    return 0;
}

编译,运行效果如下。可以看到修改例子后,解决了截断问题:

方法二、使用basic_string<unsigned char>

std::string定义在/usr/include/c++/11/bits/stringfwd.h中:

cpp 复制代码
  /// A string of @c char
  typedef basic_string<char>    string;   

#ifdef _GLIBCXX_USE_WCHAR_T
  /// A string of @c wchar_t
  typedef basic_string<wchar_t> wstring;   
#endif

#ifdef _GLIBCXX_USE_CHAR8_T
  /// A string of @c char8_t
  typedef basic_string<char8_t> u8string;
#endif

#if __cplusplus >= 201103L
  /// A string of @c char16_t
  typedef basic_string<char16_t> u16string; 

  /// A string of @c char32_t
  typedef basic_string<char32_t> u32string; 
#endif

从上面可以看到,std::string其实就是basic_string<char>。所以我们可以定义basic_string<unsigned char>为ustring:

cpp 复制代码
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <bits/stdc++.h>

using namespace  std;

//typedef basic_string<char>    string;
typedef basic_string<unsigned char> ustring;


int main()
{
    const unsigned char cbuffer[]={0x61,0x62,0x63,0x0,0x64};
    ustring ustr = cbuffer;
    //std::cout << ustr << std::endl;  //error
    printf("%s\n", ustr.c_str());

  return 0;
}

编译,运行效果如下:

这种方法并不是很推荐,因为使用std::cout和其它流运算符无法打印basic_string<unsigned char>的内容。

参考

C++ : Idiomatic way of creating string from unsigned char array

Strings of unsigned chars

How to Convert unsigned char* to std::string in C++?

Thread: Convert `const unsigned char \[\]' to `std::string'

相关推荐
Chester_19995 小时前
CSP202203C.计算资源调度器
开发语言·数据结构·c++·蓝桥杯
裕晟资质规划6 小时前
武器装备科研生产单位保密资质申请方法论:条件模型与流程拆解
人工智能·算法
EXI-小洲6 小时前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载
java·开发语言·spring boot·word
会周易的程序员7 小时前
给 PLC 写一个字节码虚拟机:STVM 虚拟机架构设计
开发语言·c++·虚拟机·软plc·iec61131·stvm
机器视觉知识推荐、就业指导7 小时前
为什么老说“纯 Qt 没啥就业市场”?
开发语言·qt
telepan7 小时前
Qt 开发避坑与性能指南:如何优雅、安全地定义全局常量字符串?
开发语言·qt
小灰灰搞电子7 小时前
完全驾驭 Qt 与数据库:C++ ORM 框架 QxOrm 原理与实践指南
数据库·c++·qt
QT界面美化性能优化7 小时前
QT+AI:使用AI技术为QT应用程序赋能
c++·人工智能·qt·opencv·qt教程·qt6.3
m0_695251497 小时前
Qt MaintenanceTool 使用国内镜像加速下载(超详细教程)
开发语言·qt
en.en..8 小时前
C语言 标准输入 / 输出缓冲区
算法