C++ std::string与char[]的互转

C++ std::string与char[]的互转

  • [1 char[]转std::string](#1 char[]转std::string)
  • [2 std::string转char[]](#2 std::string转char[])

1 char[]转std::string

cpp 复制代码
char arry[5] = {0x03,0xaf,0x34,0x65,0x55};
//方法1
std::string strArry(arry);
//方法2
std::string strArry(arry,sizeof(arry));

2 std::string转char[]

cpp 复制代码
std::cout << "hex:" << std::hex;
char* bytes = new char[strArry.length()+1];
std::strcpy(bytes, strArry.c_str());
for (int i = 0; i < strArry.length(); i++)
	std::cout << (bytes[i] & 0xff);
std::cout << std::dec << std::endl;
相关推荐
hixiong12320 小时前
C# OpenCVSharp实现Hand Pose Estimation Mediapipe
开发语言·opencv·ai·c#·手势识别
集成显卡20 小时前
AI取名大师 | PM2 部署 Bun.js 应用及配置 Let‘s Encrypt 免费 HTTPS 证书
开发语言·javascript·人工智能
AI小云20 小时前
【Numpy数据运算】数组间运算
开发语言·python·numpy
yongui4783421 小时前
基于深度随机森林(Deep Forest)的分类算法实现
算法·随机森林·分类
是苏浙21 小时前
零基础入门C语言之C语言实现数据结构之单链表经典算法
c语言·开发语言·数据结构·算法
纵有疾風起21 小时前
C++—vector:vecor使用及模拟实现
开发语言·c++·经验分享·开源·stl·vector
橘颂TA21 小时前
【剑斩OFFER】算法的暴力美学——点名
数据结构·算法·leetcode·c/c++
任子菲阳1 天前
学Java第四十三天——Map双列集合
java·开发语言
wearegogog1231 天前
基于MATLAB的谷物颗粒计数方法
开发语言·matlab
Jackson@ML1 天前
360度看C#编程语言
开发语言·c#