[REBOL] Re: 32 bit - dword values
From: larry:ecotope at: 23-Dec-2000 17:16
Hi Paul
This is a confusing and poorly documented part of REBOL. There are two
problems. First to-hex returns an issue! NOT a binary!, so a conversion is
required. Second REBOL only supports signed 32-bit integers, so the upper
half of the unsigned binary range is represented as negative integers. So we
have to use the decimal! type to represent unsigned 32-bit greater than 2 **
31 - 1 or 2147483647 which is the largest signed 32-bit, and then convert to
the correct negative integer by subtracting 2 ** 32. It would be nice if RT
added direct binary conversion for integers to the language.
Here is a function which will work. As you can see, the required steps are
not so obvious. If you write these to a file or port, be sure to use binary
mode.
; Convert unsigned 32-bit integer represented
; as REBOL decimal! or integer! to binary form.
; On Intel platform the bytes need to be
; reversed to be read by other programs.
; The maximum value is 2 ** 32 - 1 which is
; 4294967295 or #{FFFFFFFF} in binary.
unsigned-to-binary: func [n [number!] /rev][
if n > (2 ** 31 - 1) [n: n - (2 ** 32)]
n: load join "#{" [form to-hex to-integer n "}"]
either rev [head reverse n][n]
]
>> unsigned-to-binary 5555555555
== #{4B230CE3}
>> unsigned-to-binary (2 ** 32 - 1)
== #{FFFFFFFF}
HTH
-Larry
----- Original Message -----
From: Paul Tretter <[ptretter--primary--net]>
To: <[rebol-list--rebol--com]>
Sent: Friday, December 22, 2000 9:53 PM
Subject: [REBOL] 32 bit - dword values
> How do we handle 32 bit numbers when trying to convert them. It seems
that special precautions must be taken to prevent math overflow errors. Can
someone from RT tell us how we should handle these issues. For example if I
do a to-hex 5555555555 I get an error: