ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Custom ROS message with unit8[] in python!

asked 2020-01-17 08:16:35 -0600

Farid gravatar image

updated 2020-01-17 08:19:52 -0600

I have a PacketMsg.msg as follows in my package:

uint8[] buf

I would like to decode /topic with this sample python script;

import rospy
from ouster_ros.msg import PacketMsg

def cb(msg):
    rospy.loginfo('len_msg: {}, type_msg: {}'.format(len(msg.buf), type(msg.buf)))
    rospy.loginfo('msg: {}'.format(msg.buf)) 

def main():
    rospy.Subscriber('/topic', PacketMsg,cb)

if __name__ == "__main__":

This is the output printed in terminal:

[INFO] [1579270050.540915]: len_msg: 49, type_msg: <type 'str'>
[INFO] [1579270050.541343]: msg: ��H?�~�a�B��`����g=��?M��

whereas, $ rostopic echo /topic returns:

buf: [47, 185, 250, 245, 155, 0, 0, 0, 54, 238, 166, ....................... , 23, 154, 0, 0]

Does anybody know how to change such <type 'str'> to a list for further usages?


edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2020-01-18 01:06:01 -0600

Dirk Thomas gravatar image

In Python a str can be iterated and indexed in the same way as a list: msg.buf[i]

Each 8-bit character can be converted into a number using ord().

To get the same result as you see on the console you can do a list comprehension: [ord(c) for c in msg.buf]

edit flag offensive delete link more


Hi! I have the same problem, I get a series of letters instead of number but I am coding with c++. How can I solve the same issue? Many thanks

v.leto gravatar image v.leto  ( 2021-11-26 11:35:06 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2020-01-17 08:16:35 -0600

Seen: 665 times

Last updated: Jan 18 '20