roslibjs icon indicating copy to clipboard operation
roslibjs copied to clipboard

uint8[] received as string

Open yesennes opened this issue 9 years ago • 2 comments

Using roslibjs, I subscribed to an sensor_msgs/Image message and attempted to use the data, which should have been uint8[]. I was somewhat surprised to find it was a string. I understand that a string is a char[] and a char is a uint8, so I tried to use charCodeAt to interpret the string as a uint8 array. However, it doesn't seem to be a direct correspondence.

It seems to be consistent, that is the same uint8[] always comes out as the same string, but I couldn't figure out how to get the original data back.

yesennes avatar Dec 21 '16 16:12 yesennes

uint8[] from rosbridge server is encoded as b64 base. so you may need to decode to get a original message.

jihoonl avatar Jan 17 '17 08:01 jihoonl

If someone search a fast solution, I managed to decode it in 2 steps.

lets imagine, our uint8[] is result.pose:

1- var p = atob(result.pose);

With this, the variable is decoded in strange chars. In my case i was watching triangles.

2- p.charCodeAt(3)

This way the char is decoded to a int again. I did several tries and it works.

If you want to have the full array full decoded, I'm just thinking about do a for loop.

Solrac3589 avatar Jan 11 '18 11:01 Solrac3589