---
title: "XYZRGB型PCD文件中rgb字段的加包与拆包"
author: "foxcoder"
date: "2013-12-18T05:39:48Z"
tags: []
categories: ["开源软件"]
slug: "xyzrgb-pcd-rgb"
draft: false
lastmod: "2013-12-18T05:39:48Z"
id: 1991160
url: "https://foxcoder.iteye.com/blog/1991160"
---

```

```

```
 #include<iostream>
using namespace std;
void main()
{
	//--------------PCD文件RGB字段的拆包和加包------------
	//加包（已知RGB，求float）
	int r=109,g=114,b=134;
	int rgb = ((int)r << 16 | (int)g << 8 | (int)b); 
	float frgb = *reinterpret_cast<float*>(&rgb); 


	cout<<"frgb="<<frgb<<endl
		<<"rgb="<<rgb<<endl
		<<"r="<<r<<endl
		<<"g="<<g<<endl
		<<"b="<<b<<endl;
	//拆包（已知float，求RGB）
	int nrgb = *reinterpret_cast<int*>(&frgb); 
	int nr = (nrgb >> 16) & 0x0000ff; 
	int ng = (nrgb >> 8) & 0x0000ff; 
    int nb = (nrgb) & 0x0000ff; 

	cout<<"frgb="<<frgb<<endl
		<<"nrgb="<<rgb<<endl
		<<"nr="<<r<<endl
		<<"ng="<<g<<endl
		<<"nb="<<b<<endl;
}
```

 

XYZRGB型的PCD文件数据格式为

**X                Y                   Z                   rgb**

前三个都好说，最后一个rgb是用一个float来表示的，原本存储三个颜色信息的数值用一个数据来表示，明显是用到了位储存，所以考虑位操作。做了一个简单的VC程序，不做多的解释。

 

**运行结果：**

![](http://dl2.iteye.com/upload/attachment/0092/2491/a2445724-5e46-3531-a723-b27785cc6ae9.jpg)