Your Digital Media Has Never Looked So Good

 
kmikz
Topic Author
Posts: 6
Joined: Wed May 31, 2017 4:47 am

Pack data into binary string

Wed Dec 06, 2017 12:29 pm

Hello,

I am trying to find a solution to pack data into a binary string similar to the pack function in php and perl. Specifically into unsigned short (always 16 bit, big endian byte order) format. I am trying to use it to write data to network socket so the big endian byte order is important. If someone could point me into the right direction I would appreciate it.

Thank you
 
User avatar
RokuNB
Posts: 306
Joined: Fri Mar 31, 2017 2:22 pm

Re: Pack data into binary string

Wed Dec 06, 2017 3:13 pm

Use https://sdkdocs.roku.com/display/sdkdoc/roByteArray , probably by using .push() (ifArray interface) on octets/bytes.
To chop into bytes, use bitwise ops and/or/<</>>
 
kmikz
Topic Author
Posts: 6
Joined: Wed May 31, 2017 4:47 am

Re: Pack data into binary string

Thu Dec 07, 2017 8:30 am

Hi RokuNB,
Thank you for the quick reply.
This is what I came up with based on a javascript implementation of the function
Function pack(value As Integer)
 result = ""
 result += chr(value >> 8 and 255)
 result += chr(value and 255)
 return result
End Function

For PHP the function pack("n", 333) will return a string "[SOH]M" where [SOH] is ascii symbol SOH. Roku will return "?M"
I am missing something and I can't figure what. 

Later edit: found my issue.
value >> 8 and 255 evaluates to 0 and chr(0) returns empty string if the specified value is 0 or an invalid Unicode value, as per the documentation. I need to preppend the ASCII 0 character, which is NUL. Any ideas how to do that?
 
kmikz
Topic Author
Posts: 6
Joined: Wed May 31, 2017 4:47 am

Re: Pack data into binary string

Thu Dec 07, 2017 12:23 pm

ab = CreateObject("roByteArray")
ab.FromHexString("003C")
?ab
aba = ab.ToAsciiString()
?type(aba)
?aba.len()

ba = CreateObject("roByteArray")
ba.push(0)
ba.push(60)
bab = ba.ToAsciiString()
?ba
?bab
?bab.len()

This will return
<Component: roByteArray> =
[
    0
    60
]
String
 0
<Component: roByteArray> =
[
    0
    60
]

 0


There is no way that I can see to obtain the 2 character string "[nul]<" that has the ascii codes 0060 or the hex code 003C. Does anybody have a workaround? 
 
User avatar
RokuNB
Posts: 306
Joined: Fri Mar 31, 2017 2:22 pm

Re: Pack data into binary string

Sat Dec 09, 2017 5:41 pm

kmikz wrote:
[...]
There is no way that I can see to obtain the 2 character string "[nul]<" that has the ascii codes 0060 or the hex code 003C. Does anybody have a workaround? 

If you start thinking of roByteArray as "binary string" (vs roString as "text string"), does that help?
Note how ifSocket .send/.receive() work with roByteArray and .sendStr/.receiveStr() work with texts...
 
kmikz
Topic Author
Posts: 6
Joined: Wed May 31, 2017 4:47 am

Re: Pack data into binary string

Sun Dec 10, 2017 12:22 am

Thank you, that was the solution I was looking for. My mistake was trying to do it like PHP and send string, I was not aware you could send byte arrays.

Who is online

Users browsing this forum: No registered users and 7 guests