Question

I'm receiving UDP packets from a server ( exactly: Open Sound Control packets). i'm storing those packets in a ByteArray.

I want to convert this ByteArray into String so i can exploit the data received. I tried a lot of conversion but each time i'm having non readable charachters.

Here is the code :

| server peerAddr |
server := SocketAccessor newUDPserverAtPort: 3333.
peerAddr := IPSocketAddress new.
buffer := ByteArray new: 1024.
[ server readWait.
server receiveFrom: peerAddr buffer: buffer.

Transcript show: (buffer asString) ; cr ; flush. ] repeat.

I also tried the following conversion but in vain:

buffer asByteString.
buffer asStringEncoding:#UTF8.
buffer asStringEncoding:#UTF16.
buffer  asString.
buffer  asBase64String.
buffer  asFourByteString
buffer withEncoding: #ASCII

Here is the string output : enter image description here

Any help?

Additional info: The received data is open sound control data so it has a specific formatting, that's why it's diplayed like that, i need to parse ints, floats, strings, whitin a specific bytearray indexs. Does anyone recomand a package that offer those possibilities ?

Thx in advance.

Was it helpful?

Solution 3

You probably can borrow a lot of the code for Squeak's OSC package: http://opensoundcontrol.org/implementation/squeak-osc

OTHER TIPS

if you want to read the data from the byte array, use the UninterpretedBytes class for that.

You can do:

ubytes := UninterpretedBytes from: aByteArray. ubytes doubleAt: 5.

stuff like that. you can also use the uninterpreted bytes to read a string from the bytes.

The correct way to convert bytes to string is definitely applying the right character encoding. The following

(65 to: 75) asByteArray asStringEncoding: #UTF8

should yield

'ABCDEFGHIJK'

Using #asStringEncoding: is the right way to do this. However looking at your screen capture it seems that the bytes you're receiving are not a straight string. There's probably some binary packet format that you need to take apart first and then only decode into strings those parts that you know are actually utf8 encoded (or whatever the encoding is).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top