将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'

相关推荐
仰泳的熊猫1 小时前
题目2570:蓝桥杯2020年第十一届省赛真题-成绩分析
数据结构·c++·算法·蓝桥杯
似水明俊德4 小时前
02-C#.Net-反射-面试题
开发语言·面试·职场和发展·c#·.net
无极低码4 小时前
ecGlypher新手安装分步指南(标准化流程)
人工智能·算法·自然语言处理·大模型·rag
软件算法开发5 小时前
基于海象优化算法的LSTM网络模型(WOA-LSTM)的一维时间序列预测matlab仿真
算法·matlab·lstm·一维时间序列预测·woa-lstm·海象优化
Thera7775 小时前
C++ 高性能时间轮定时器:从单例设计到 Linux timerfd 深度优化
linux·开发语言·c++
superior tigre5 小时前
22 括号生成
算法·深度优先
炘爚6 小时前
C语言(文件操作)
c语言·开发语言
阿蒙Amon6 小时前
C#常用类库-详解SerialPort
开发语言·c#
凸头6 小时前
CompletableFuture 与 Future 对比与实战示例
java·开发语言