Inferno
Inferno

Reputation: 47

Erlang decode binary data from packet

I get an UDP packet, like so:

<<83,65,77,80,188,64,171,138,30,120,105,0,0,0,10,0,4,0,0,0,84,101,115,116,15,0,0,0,82,101,122,111,110,101,32,82,111,108,101,80,108,97,121,11,0,0,0,83,97,110,32,65,110,100,114,101,97,115>>

How can I decode packet if I know that I can remove first 11 bytes, and the 12-13 byte contains amount of players online on the server (Byte width is 2), how can I get this amount?

UPD
Maybe I send incorrect packet...
SAMP Query
So, I send:

<<$S,$A,$M,$P,188,64,172,136,7808:16,$i>>

For server 188.64.172.136:7808, and I get

<<83,65,77,80,188,64,172,136,30,128,105,0,0,0,10,0,4,0,0,0,84,101,115,116,15,0,0,0,82,101,122,111,110,101,32,82,111,108,101,80,108,97,121,11,0,0,0,83,97,110,32,65,110,100,114,101,97,115>>

Upvotes: 1

Views: 953

Answers (2)

Artefact2
Artefact2

Reputation: 7634

You can use the bit syntax and clever pattern matching to get the result:

<<_:11/bytes, NumberOfPlayers:16/integer-big, _/binary>> = <<83,65,77,80,188,64,171,138,30,120,105,0,0,0,10,0,4,0,0,0,84,101,115,116,15,0,0,0,82,101,122,111,110,101,32,82,111,108,101,80,108,97,121,11,0,0,0,83,97,110,32,65,110,100,114,101,97,115>>,
NumberOfPlayers.

Upvotes: 2

Diego Sevilla
Diego Sevilla

Reputation: 29021

If your packet binary is stored in P, you can do something like (assuming big endian):

<<NumberOfPlayersOnline:16/big>> = binary:part(P,11,2).

The result is in NumberOfPlayers.

Upvotes: 1

Related Questions