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

----- Original Message -----
From: "Joel Neely" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, May 03, 2001 12:18 PM
Subject: [REBOL] Re: Poke and Pick and binary! - bug


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


-- 
To unsubscribe from this list, please send an email to
[EMAIL PROTECTED] with "unsubscribe" in the 
subject, without the quotes.

Reply via email to