[ros-users] [Discourse.ros.org] [Client Libraries] Python3 and strings

Kartik Mohta ros.discourse at gmail.com
Tue Aug 8 03:17:46 UTC 2017



>From the generated python code for a msg, when serializing the message into a buffer to send it, ROS encodes the string field as a utf-8 string (x is a string field in the ROS msg):

    _x = self.x
    if python3 or type(_x) == unicode:
      _x = _x.encode('utf-8')

And similarly, when deserializing the received buffer, it is converted into a Python str with utf-8 encoding:

    if python3:
      self.x = str[start:end].decode('utf-8')
    else:
      self.x = str[start:end]

So on the user side, you just need to make sure that the encoding for the string you're sending is utf-8.

With that in mind, that block from the msg wiki page seems sufficient to me:
> 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, both encoding and decoding are automatic.





---
[Visit Topic](https://discourse.ros.org/t/python3-and-strings/2392/4) or reply to this email to respond.




More information about the ros-users mailing list