2

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.

SolidSnake87
  • 343
  • 3
  • 12
  • If you inspect the received ByteArray and each byte, is there any ASCII character code point at all? Like 64 for $A asInteger etc. Try to find out this way in which encoding the received string is. – Janko Mivšek Feb 28 '13 at 09:22
  • In fact, the received bytearray does not contain only Strings, i added some intel in the questions. – SolidSnake87 Feb 28 '13 at 10:50

3 Answers3

3

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.

Karsten
  • 2,772
  • 17
  • 22
3

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).

Martin Kobetic
  • 221
  • 1
  • 2
2

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

codefrau
  • 4,583
  • 17
  • 17
  • Yes, that's what itried to do, but in squeak ther is a lot of different packages that do not exists in visual works. Especially handling byte streams and converting types. Have you found any visualworks implementation before ? because it resolve all the problem. – SolidSnake87 Mar 01 '13 at 13:06
  • Siren supposedly has OSC support http://fastlabinc.com/Siren/main.html Also, VWOSC: http://web.archive.org/web/20080411133836/http://www.mat.ucsb.edu/~c.ramakr/illposed/vwosc.html – codefrau Mar 01 '13 at 13:23
  • Okey i had already Siren and VWOSC, and the code is only for sending OSC packets, in my case i'm the receiver. And VW OSC source is in .sit extension i think it's for Mac, i'm taking a look at it with Aladdin Expander, but anyway i will continue searching, and i will be thankful if you have any new ideas. Thx a lot. – SolidSnake87 Mar 01 '13 at 13:53