简体   繁体   中英

Converting Open Sound Control ByteArray into String in Smalltalk VisualWorks 7.9.1

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 : 在此输入图像描述

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.

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

你可能可以为Squeak的OSC包借用很多代码: http//opensoundcontrol.org/implementation/squeak-osc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM