Static cast equivalent in python

My problem is the following:

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[].

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()

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

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

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

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

Cheers,

Panos

Answers


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

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

(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)

Need Your Help

Maximum data size when sending data via BTLE on iOS

ios objective-c bluetooth-lowenergy core-bluetooth data-transfer

I am currently implementing an iOS application that uses CoreBluetooth to transfer data between 2 devices. For example, to send data from the central to the peripheral, I use this code:

How can I add a JPanel directly over another one in an array?

java swing jpanel

I'm trying to make a board game for my CS course.. I've built the basic board by putting JPanels next to each other, now I want to add some checkers on top of those JPanels so that whenever I move ...