C++char*转string,同时输出Ascii码类型和Binary类型

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() 
{
	int num = 3;
	int lengthPointCloud = 3 * sizeof(float) + 3 * sizeof(unsigned char);
	//头内容Ascii码格式
	stringstream header_binary;
	header_binary << "ply" << endl
		<< "format binary_little_endian 1.0" << endl
		<< "comment PCL generated" << endl
		<< "element vertex " << num << endl
		<< "property float x" << endl
		<< "property float y" << endl
		<< "property float z" << endl
		<< "property uchar red" << endl
		<< "property uchar green" << endl
		<< "property uchar blue" << endl
		<< "end_header" << endl;
	//点云数据Binary格式
	char *pointCloud_binary = new char[num * lengthPointCloud];

	float *X = new float[num] {0};
	float *Y = new float[num] {0};
	float *Z = new float[num] {0};
	unsigned char *R = new unsigned char[num] {0};
	unsigned char *G = new unsigned char[num] {0};
	unsigned char *B = new unsigned char[num] {0};
	X[0] = -11.122746;
	X[1] = -2.725141;
	X[2] = 12.04;
	Y[0] = 10.255;
	Y[1] = 7.017;
	Y[2] = -12.04;
	Z[0] = -19;
	Z[1] = -13;
	Z[2] = -18;
	for (int i = 0; i < num; i++) {
		R[i] = 255;
		G[i] = 0;
		B[i] = 0;
	}

	for (int i = 0; i < num; i++) {
		memcpy(pointCloud_binary + i * 15, &X[i], sizeof(float));
		memcpy(pointCloud_binary + i * 15 + 4, &Y[i], sizeof(float));
		memcpy(pointCloud_binary + i * 15 + 8, &Z[i], sizeof(float));
		memcpy(pointCloud_binary + i * 15 + 12, &R[i], sizeof(unsigned char));
		memcpy(pointCloud_binary + i * 15 + 13, &G[i], sizeof(unsigned char));
		memcpy(pointCloud_binary + i * 15 + 14, &B[i], sizeof(unsigned char));
	}

	string pointCloud;
	pointCloud = header_binary.str();
	string pointCloudToString(num * lengthPointCloud, ' ');

	for (int i = 0; i < num * lengthPointCloud; i++)
		pointCloudToString[i] = pointCloud_binary[i];
	pointCloud += pointCloudToString;
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值