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

相关推荐
s090713612 小时前
FPGA加速:Harris角点检测全解析
图像处理·算法·fpga开发·角点检测
前端程序猿之路12 小时前
30天大模型学习之Day 2:Prompt 工程基础系统
大数据·人工智能·学习·算法·语言模型·prompt·ai编程
极客小云12 小时前
【Python pip换源教程:国内镜像源配置方法(清华/阿里云/中科大源)】
开发语言·python·pip
a35354138212 小时前
设计模式-原型模式
开发语言·c++
Tinachen8812 小时前
YonBIP旗舰版本地开发环境搭建教程
java·开发语言·oracle·eclipse·前端框架
liulilittle12 小时前
libxdp: No bpffs found at /sys/fs/bpf
linux·运维·服务器·开发语言·c++
hqwest12 小时前
码上通QT实战07--主窗体消息栏设计
开发语言·qt·qt事件·主窗体·stackedwidget
hqwest12 小时前
码上通QT实战06--导航按钮事件
开发语言·qt·mousepressevent·qfont·qpainter·qlineargradient·setbrush
星火开发设计12 小时前
堆排序原理与C++实现详解
java·数据结构·c++·学习·算法·排序算法
shughui12 小时前
实现Python多版本共存
开发语言·python·pip