ByteArray to string...

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

ByteArray to string...

talios@gmail.com
Heya - I'm trying to read an entry from the registry which is stored as
a ByteArray containing the text I want, padded with nulls, so the string
http comes out as "h <null> t <null> t <null> p".

Is there anything built into Dolphin to read/parse this as a normal String?

I did try just looping through skipping each 2nd byte, but I can't seem
to spot a method going from the SmallInteger that comes out of the array
to a character.

Mmmm.


Reply | Threaded
Open this post in threaded view
|

Re: ByteArray to string...

talios@gmail.com
Mark Derricutt wrote:

> I did try just looping through skipping each 2nd byte, but I can't
> seem to spot a method going from the SmallInteger that comes out of
> the array to a character.

Well I got it working with:

byteArrayWithNullsToString: aByteArray
    "Convert registry null spaced bytearray to a string."

    | s offset |
    s := String new.
    offset := 1.
    [offset < aByteArray size] whileTrue:
            [s := s , (aByteArray byteAtOffset: offset - 1) asCharacter
asString.
            offset := offset + 2].
    ^s

Is there a more optimized way of doing this?


Reply | Threaded
Open this post in threaded view
|

Re: ByteArray to string...

Bill Schwab-2
Mark,

> > I did try just looping through skipping each 2nd byte, but I can't
> > seem to spot a method going from the SmallInteger that comes out of
> > the array to a character.
> Is there a more optimized way of doing this?

Hard to say.  I would have coded it something like this:

| in out |
out := String writeStream.
in := aByteArray readStream
[ in atEnd ] whileFalse:[
    out nextPut:in next asCharacter.
    in next.
].
^out contents.

Caveat: I haven't tested this, so it might not work at all, but some variant
on it hopefully will.

You might also be able to create a UnicodeString from the data, and then
send #asString.  However, I suspect that UnicodeString follows the (jump on
me if this is unfair) Microsoft practice of unicode==wideCharacter rather
than using slicker encodings.  Will it remain that way, or improve and break
your code?  It might be best to roll your own.

Have a good one,

Bill

--
Wilhelm K. Schwab, Ph.D.
[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: ByteArray to string...

Bill Dargel
In reply to this post by talios@gmail.com
Mark Derricutt wrote:
 > Is there a more optimized way of doing this?

Well this is certainly shorter:

        (#[65 0 0 66 0 67 0] copyWithout: 0) asString

-Bill