[REBOL] Re: Poke and Pick and binary! - bug
From: joel:neely:fedex at: 3-May-2001 14:18
Anton wrote:
...
> >> to-binary to-integer to-binary 20
> == #{3132383438} ; spinning out of control... etc
>
> Must be a bug.
>
Not a bug.
Think of binary as a string represented in hexadecimal.
>> to-char #{14}
** Script Error: Invalid argument: #{14}
** Where: to-char
** Near: to char! :value
>> series? #{14}
== true
>> first #{14}
== 20
So #{14} is a series of bytes (of length 1). If we want to
convert to characters, we have to get one character-sized part.
>> to-char first #{14}
== #"^T"
This means that there are a bunch of expressions that numerically
evaluate to 20...
>> to-integer #{14}
== 20
>> to-string #{14}
== "^T"
>> to-integer #"^T"
== 20
>> to-integer first "^T yadda, yadda, yadda"
== 20
And back again...
>> to-binary "^T"
== #{14}
Now, remember your ASCII codes:
>> to-integer #"2"
== 50
>> to-integer #"0"
== 48
>> to-hex 50
== #00000032
>> to-hex 48
== #00000030
So, when you convert directly from integer to binary, you're getting
an implicit string conversion in the middle:
>> to-binary 20
== #{3230}
>> to-char first to-binary 20
== #"2"
>> to-char second to-binary 20
== #"0"
>> to-string to-binary 20
== "20"
> > Can someone help me with the following error? Why can I swap
> > elements of a string! with pick and poke, but not the elements
> > of a binary! ?
> >
> > >> list: to-binary "1234567890"
> > == #{31323334353637383930}
> > >> poke list 1 pick list 2
> > ** Script Error: Invalid argument: 50
> > ** Where: halt-view
> > ** Near: poke list 1 pick list
>
Let's look at your data another way
>> foo: to-binary "1234567890"
== #{31323334353637383930}
>> pick foo 2
== 50
>> second foo
== 50
So what we're getting is a byte promoted to a full-sized integer.
To poke it back, let's force it back down to byte width.
>> poke foo 1 to-char pick foo 2
== #{32323334353637383930}
>> to-string foo
== "2234567890"
Hope this helps!
-jn-