python中的静态转换等效项 [英] Static cast equivalent in python

查看:56
本文介绍了python中的静态转换等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题如下:

我正在使用 ROS,我目前正在尝试从 Kinect 传感器的 PointCloud 读取值.值的类型为 PointCloud2,PointCloud2.data 保存 PointCloud 的数据,类型为 uint8[].

I am using ROS and I am currently trying to read values from a PointCloud of a Kinect sensor. The values are of type PointCloud2 and PointCloud2.data holds the data of the PointCloud and is of type uint8[].

在我的项目中,我目前正在使用 python 并且我已成功注册和检索数据.我的问题是,当试图检索它们时,python 将 uint8[] 数组解析为字符串.

In my project I am currently using python and I have successfully registered and retrieved the data. My issue is that when trying to retrieve them python parses the uint8[] array as strings.

我遇到问题的脚本的当前部分如下:

The current part of the script where I have the problem is the follwowing:

def PointCloud(self, PointCloud2):
    self.lock.acquire()
        for x in PointCloud2.data:
            print x
    self.lock.release()

我知道在 C++ 中我可以做类似的事情

I know that in c++ I can do something like

 std::cout << static_cast< int >( u );

获取无符号整数的值.是否有等同于 python 的静态转换?

To get the value of the unsigned integer. Is there a static cast equivalent to python?

在我的例子中,print x 输出 ASCII 字符.我如何检索它的 int 值?

In my case print x outputs ASCII characters. How could I retrieve the int value of that?

干杯,

全景

推荐答案

使用struct.unpack得到x中二进制数据编码的整数值.

Use struct.unpack to get the integer value encoded by the binary data in x.

print struct.unpack("!I", x)

(我假设 x 包含一个以网络字节顺序排列的 4 字节整数;如果您需要 以外的格式,请查阅 struct 模块的文档>!I.)

(I'm assuming x contains a 4-byte integer in network byte order; consult the documentation for the struct module if you need a format other than !I.)

更新:我错过了你有一个无符号字节数组;在这种情况下,使用

Update: I missed that you have an array of unsigned bytes; in that case, use

struct.unpack("B", x)

这篇关于python中的静态转换等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆