Ask Your Question
0

How do I Use Unicode Strings in pubish/subscribe?

asked 2017-08-30 01:52:22 -0600

NGI gravatar image

I want to publish and subscribe unicode strings (multibyte text). but, If I did it then UnicodeEncodeError was Occured.

Can I use multibyte text in publish and subscribe ?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2017-08-30 02:54:29 -0600

gvdhoorn gravatar image

Can I use multibyte text in publish and subscribe ?

I don't think that it is supported in ROS1. From ros/wiki/msg - Fields:

unicode strings are currently not supported as a ROS data type. utf-8 should be used to be compatible with ROS string serialization. In python 2, this encoding is automatic for unicode objects, but decoding must be done manually. In python 3, when using str, both encoding and decoding using 'utf-8' in the generated message code.

One workaround I can think of is to instead of using the string type, using the uint8 type. Serialise your wide-string yourself into that uint8 array, publish it, and in the subscriber recreate the wide-string.

That is not nice (as you lose the type info), but could work.

edit flag offensive delete link more

Comments

Thank you for your answer.

I understood it.

For a while, I'll try it.

NGI gravatar imageNGI ( 2017-08-30 03:00:52 -0600 )edit

I checked it.Thanks!

And, I found other way. I can recieve Unicode Message by above sequence.

in Publish Node: Original text [unicode] => text.encode('utf-8') [change to str] => publish in Subscribe Node: subscribe => text=data.data [str] => text.decode('utf-8') [unicode]

Thanks!

NGI gravatar imageNGI ( 2017-08-30 03:32:42 -0600 )edit
1

If that works, then you should post it as an answer instead of a comment.

gvdhoorn gravatar imagegvdhoorn ( 2017-08-30 03:38:31 -0600 )edit
0

answered 2017-08-30 04:37:59 -0600

NGI gravatar image

I can recieve Unicode Message by above sequence.

in Publish Node: Original text [unicode] => text.encode('utf-8') [change to str] => publish

in Subscribe Node: subscribe => text=data.data [str] => text.decode('utf-8') [unicode]

Thanks!

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2017-08-30 01:52:22 -0600

Seen: 910 times

Last updated: Aug 30 '17