Poke and Pick and binary! - bug
[1/4] from: arolls:bigpond:au at: 4-May-2001 2:51
I see it too.
Further;
; binary is coded in hexadecimal by default
>> to-integer #{14}
== 20 ; ok
>> to-binary 20
== #{3230} ; That's not right
>> to-integer to-binary 20
== 12848 ; I expected 20, the same value, back.
>> to-binary to-integer to-binary 20
== #{3132383438} ; spinning out of control... etc
Must be a bug.
REBOL/View/Pro 1.1.0.3.1 16-Apr-2001
Anton.
[2/4] 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
<<quoted lines omitted: 6>>
> > ** 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-
[3/4] from: larry:ecotope at: 3-May-2001 12:48
Hi Joel
Good explanation. There is one more feature which can be handy. To directly
convert an integer in the range 0 to 255 to a 1-byte binary, just put it in
a block:
>> to-binary [255]
== #{FF}
>> to-integer to-binary [255]
== 255
As you noted the last line will return a full 4-byte REBOL integer (same as
long in C). REBOL does not natively support 2-byte integers, 1-byte integers
can be handled as char.
-Larry
[4/4] from: joel:neely:fedex at: 3-May-2001 15:40
Larry Palmiter wrote:
> Hi Joel
> Good explanation. There is one more feature which can be handy. To directly
<<quoted lines omitted: 4>>
> >> to-integer to-binary [255]
> == 255
Excellent! I hadn't thought of that trick (and probably wouldn't
ever have ;-)
-jn-
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted