Search code examples
formattingmqttmosquitto

What is the format for mosquitto_sub to display spaces between bytes in a binary message?


This option produces no spacing between bytes in a binary message:

paul@desktop:~$ mosquitto_sub -t out -F '%x' -v
3031320033340035
3031320033340035

I hoped this would work:

paul@desktop:~$ mosquitto_sub -t out -F '%3x'
 30
 30

but it truncates the message. What is a solution to get some separation between the bytes, e.g. 30 31 32 00 33 34 00 35?

BTW, I've read https://mosquitto.org/man/mosquitto_sub-1.html#outputformat and How to display binary MQTT data?.


Solution

  • This is not currently possible within mosquitto_sub, however it can be done with a sed command:

    mosquitto_sub -h test.mosquitto.org -t \$SYS/broker/uptime -F %x | sed 's/\(..\)/\1 /g'
    

    Having said that, the precision format option is currently unused for hex payloads, so adding support for mosquitto_sub -F %.4x ... seems like a good idea. That'll be in the next (long overdue) feature version.

    For example:

    $ mosquitto_sub -F %.4x ...
    3733 3238 3738 2073 6563 6f6e 6473