# Unpacking rawbytes to string

**URL:** <https://tech-community.robotics.abb.com/t/unpacking-rawbytes-to-string/7360>\
**Category:** RobotStudio\
**Tags:** robotstudio, rapid, socket, unpack-rawbytes\
**Created:** [September 19, 2016, 4:22pm UTC](https://tech-community.robotics.abb.com/t/unpacking-rawbytes-to-string/7360 "2016-09-19T16:22:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SteveMob](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/stevemob/32/1600_2.png) [@SteveMob](https://tech-community.robotics.abb.com/u/SteveMob)\
**Post date:** [September 19, 2016, 4:22pm UTC](https://tech-community.robotics.abb.com/t/unpacking-rawbytes-to-string/7360/1 "2016-09-19T16:22:38Z")

</div>

Hello,

The subject sort of changed on my last thread ([ABB Robotics Community - Maintenance](https://forums.robotstudio.com/discussion/comment/25839#Comment_25839)), so I thought I would just move it to this new subject.  
Summary:  
I am sending the robot arbitrary robtarget data from LabVIEW via sockets and TCP/IP. I am trying to create a method to check if the target data has any non-numeric characters in it. Best method so far was to convert robtarget to string and parse out bad characters and then convert back to robtarget. The problem with this is, if I unpack everything into robtarget data type, the bad characters turns to a “0” all the other characters after it, into a “0”. So I have to unpack the rawbytes into a string _before_ going to robtarget data.  
Question:  
How can I unpack data being sent to the robot (_sent in robtarget format_) into a string, or multiple strings (pos string, quaternion string, config string)?

Thanks for any help!  
SM

---

<div class="post-metadata">

**Author:** ![John\_Verheij](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/john_verheij/32/1724_2.png) [@John\_Verheij](https://tech-community.robotics.abb.com/u/John_Verheij)\
**Post date:** [September 20, 2016, 6:54am UTC](https://tech-community.robotics.abb.com/t/unpacking-rawbytes-to-string/7360/2 "2016-09-20T06:54:28Z")

</div>

By the way what do you mean with “_data being sent to the robot (sent in robtarget format_\*)\*”? ‘RobTarget’ is an ABB type right?

Furthermore, the idea is to convert the received data to string and then to parse the robtarget. Why not directly use the “_SocketReceive_” function?  
[https://forums.robotstudio.com/discussion/comment/25528/#Comment\_25528](https://forums.robotstudio.com/discussion/comment/25528/#Comment_25528)

Initial all bytes in ‘rawbytes’ are set to zero. Maybe it is an idea to first send the length of the robtarget (or string) and then send the actual data?

Can you show some code how you implement it currently?

---

<div class="post-metadata">

**Author:** ![SteveMob](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/stevemob/32/1600_2.png) [@SteveMob](https://tech-community.robotics.abb.com/u/SteveMob)\
**Post date:** [September 21, 2016, 1:26pm UTC](https://tech-community.robotics.abb.com/t/unpacking-rawbytes-to-string/7360/3 "2016-09-21T13:26:08Z")

</div>

John,  
What I mean by “_data being sent to the robot (sent in robtarget format_\*)\*” is that I am sending the target data from LabVIEW like this:  
[X,Y,Z],[Q1,Q2,Q3,Q4],[C1,C4,C6,Cx]. So I unpack the individual elements that make up a robtarget data.

I am using the SocketReceive function. I send all the data at once from LV, and receive it all on one line in RS:  
**SocketReceive clientSocket\RawData:=messageBytes\Time:=25;.** I then can unpack the individual elements.

How could I set all the bytes in rawbytes to zero?

What portion of code do you need to see? My entire program is almost 600 lines of code… The unpacking process can be seen on my previous thread:  
([https://forums.robotstudio.com/discussion/comment/25839#Comment\_25839](https://forums.robotstudio.com/discussion/comment/25839#Comment_25839))  
This is my attempt at unpacking the message into strings, but the numbers are coming out to be completely different than what I was sending RS:  
**UnpackRawBytes messageBytes\Network,2,targetx\ASCII:=25; ! targetx is a string data type**  
**targetx:=HexToDec(targetx);**  
**UnpackRawBytes messageBytes\Network,6,targety\ASCII:=25; ! targety is a string data type**  
**targety:=HexToDec(targety);**  
**UnpackRawBytes messageBytes\Network,10,targetz\ASCII:=25; ! targetz is a string data type**  
**targetz:=HexToDec(targetz);**

I have also just tried it like this:  
**UnpackRawBytes messageBytes\Network,2,targetx\Hex1; !targetx is a byte data**  
**targetx1:=HexToDec(ByteToStr(targetx)); !targetx1 is a string data type**  
**UnpackRawBytes messageBytes\Network,6,targety\Hex1; !targety is a byte data type**  
**targety1:=HexToDec(ByteToStr(targety)); !targety1 is a string data type**  
**UnpackRawBytes messageBytes\Network,10,targetz\Hex1; !targetz is a byte data type**  
**targetz1:=HexToDec(ByteToStr(targetz)); !targetz1 is a string data type**

When I do it like either of these methods, I unpack some numbers, but none of them are the correct numbers that I sent…

I just need to unpack the rawbytes into _string(s)_ instead of unpacking the message into data types like robtarget or num. That’s the ultimate goal right now. That way, I don’t lose any data in the unpacking process. If I unpack a non-numeric value into say a num data type, the message is parsed a “0” due to some RS internal stuff. But if I unpack it into a string, I don’t lose the data, and I can parse out the non-numeric values and then convert that string to robtarget data or num data.

Also, when unpacking messages into strings, how do I do the indexing for that? With Float4, I was just doing every 4. But I am not sure how to do the indexing with strings.

Thanks for the help,  
SM

---

<div class="post-metadata">

**Author:** ![John\_Verheij](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/john_verheij/32/1724_2.png) [@John\_Verheij](https://tech-community.robotics.abb.com/u/John_Verheij)\
**Post date:** [September 23, 2016, 10:08am UTC](https://tech-community.robotics.abb.com/t/unpacking-rawbytes-to-string/7360/4 "2016-09-23T10:08:36Z")

</div>

I think the problem is the encoding type you used in LabVIEW. For example if I send the value “a1b2” in UTF-8 format, I receive this string correctly in RS.

But if I send the values in a different format, for example UTF-16, I get the following value “ÿþa\001\00b\002\00”. It looks like it contains ‘bad’ characters, but the encoding for every digit/character is different. You can also try to create a decoder in RS…

Furthermore, most of the time you can use directly a string value with the ‘SocketReceive’ function.

```auto

SocketReceive client_socket  \RawData:=revRB;

UnpackRawBytes revRB\Network, 1, revStr\ASCII:=RawBytesLen(revRB);

```

```auto

SocketReceive client_socket \Str := revStr;

```

---

<div class="post-metadata">

**Author:** ![SteveMob](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/stevemob/32/1600_2.png) [@SteveMob](https://tech-community.robotics.abb.com/u/SteveMob)\
**Post date:** [September 23, 2016, 2:01pm UTC](https://tech-community.robotics.abb.com/t/unpacking-rawbytes-to-string/7360/5 "2016-09-23T14:01:26Z")

</div>

John,

I am not very good with LV so I cannot say for sure how the data is being sent. However, I tried receiving and unpacking as you mentioned, and yes, the format seemed to be like how you said UTF-16, similar to: _“ÿþa\001\00b\002\00”_. Is this something I can fix on the RS side or do I need to do that on the LV side? Like the decoder you mentioned? I wouldn’t even know where to start to do something like that…

If it helps at all, when I unpack them using Float4, and unpack each element and adding 4 to the index number, my numbers come out correctly (as I sent them). In LV, I am taking each element and putting them into a single, 32-bit real and converting it to a string. All those strings then get concatenated into a 1-D array of strings and then sent to RS. In RS, could I maybe unpack everything into a single float? I’m sorry, I’m not very good with the different data types and such..

Thanks so much for the help, I feel like we’re getting close to the solution  
SM

---

<div class="post-metadata">

**Author:** ![SteveMob](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/stevemob/32/1600_2.png) [@SteveMob](https://tech-community.robotics.abb.com/u/SteveMob)\
**Post date:** [September 23, 2016, 5:52pm UTC](https://tech-community.robotics.abb.com/t/unpacking-rawbytes-to-string/7360/6 "2016-09-23T17:52:39Z")

</div>

John,

I was finally able to unpack the entire message into a single string. I had to go into LabVIEW and alter the method I was sending the message.

Please go back to my original thread for further questions regarding parsing out the non-numeric characters:

> **[ABB Robotics Community - Maintenance](https://forums.robotstudio.com/discussion/comment/25839#Comment_25839)**

Thanks for the help,  
SM
