Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

32 bit - dword values

 [1/4] from: ptretter::primary::net at: 22-Dec-2000 23:53


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: ** Script Error: to-hex expected value argument of type: integer . ** Where: to-hex 5555555555 but to-hex 555555555 works fine This might be greater than a dword but illustrates my point. Anyone ran into this problem before? Paul Tretter

 [2/4] 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:

 [3/4] from: rebol:techscribe at: 15-Jan-2001 15:39


Hi Paul, try
>> to integer! #7fffffff ;- seven f's
and then try
>> to integer! #8fffffff ;- again seven f's
and
>> to integer! #ffffffff ;- eight f's
In short, REBOL supports SIGNED 32 bit integers. Hope this helps Elan Paul Tretter wrote:

 [4/4] from: ptretter:norcom2000 at: 15-Jan-2001 19:55


Hi Elan, We were at the time looking for a solution to convert dword values to an ip address for the IRCBOT we were creating. Kinners came up with the following solution: dec2tuple: func [long] [ to-tuple reduce [ to-integer long / 16777216 to-integer long / 65536 // 256 to-integer long / 256 // 256 to-integer long // 256 ] ] This function is so useful I thought RT should put this into /Core. How many people could use this? answer: alot! Thanks for your feedback. Paul Tretter EFNET #rebolgod ----- Original Message ----- From: "Elan" <[rebol--techscribe--com]> To: <[rebol-list--rebol--com]> Sent: Monday, January 15, 2001 5:39 PM Subject: [REBOL] Re: 32 bit - dword values
> Hi Paul, > try
<<quoted lines omitted: 9>>
> > > > 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:

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted