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

Converting numbers to binary form.

 [1/8] from: reboler::programmer::net at: 9-Sep-2002 10:24


Is there an easy way to represent numbers in their binary form? A work-around for 0 thru 255 is ... to-binary to-char number ... but what can one do for higher numbers?

 [2/8] from: chris:ross-gill at: 9-Sep-2002 11:57


Hi Alan,
> Is there an easy way to represent numbers in their binary form? > > A work-around for 0 thru 255 is ... > > to-binary to-char number > > ... but what can one do for higher numbers? >> to-binary to-block 255
== #{FF} - Chris

 [3/8] from: gscottjones:mchsi at: 9-Sep-2002 10:57


From: "alan parman"
> Is there an easy way to represent numbers in their binary form? > > A work-around for 0 thru 255 is ... > > to-binary to-char number > > ... but what can one do for higher numbers?
Hi, Alan, Do you mean like the hex representation of the binary form? Perhaps "to-hex" will help up to a point.
>> to-binary to-char 255
== #{FF}
>> to-hex 255
== #000000FF
>> to-hex 256
== #00000100 --Scott Jones

 [4/8] from: g:santilli:tiscalinet:it at: 9-Sep-2002 21:42


Hi Alan, On Monday, September 9, 2002, 5:24:57 PM, you wrote: ap> Is there an easy way to represent numbers in their binary form? This is the easy way, provided you're on a little-endian platform:
>> s: make struct! [num [integer!]] [0] >> integer-to-binary: func [i [integer!]] [s/num: i head reverse third s] >> integer-to-binary 100
== #{00000064}
>> integer-to-binary 100000000
== #{05F5E100} On big-endian platforms you just don't need to REVERSE it. A (probably slower, but didn't test it) multiplatform way is:
>> integer-to-binary: func [i [integer!]] [debase/base to-hex i 16] >> integer-to-binary 100000000
== #{05F5E100} Regards, Gabriele. -- Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r

 [5/8] from: reboler:programmer at: 9-Sep-2002 12:53


I want the following... 1 ==> 00000001 2 ==> 00000010 3 ==> 00000011 4 ==> 00000100 ... 255 ==> 11111111 256 ==> 100000000 ... 1000 ==> 11111010000 1001 ==> 11111010001

 [6/8] from: g:santilli:tiscalinet:it at: 10-Sep-2002 0:42


Hi Alan, On Monday, September 9, 2002, 7:53:30 PM, you wrote: ap> I want the following... [...] Use one of the two functions I provided earlier, then ENBASE the result with /BASE 2. Regards, Gabriele. -- Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r

 [7/8] from: carl:cybercraft at: 10-Sep-2002 19:38


On 10-Sep-02, alan parman wrote:
> I want the following... > 1 ==> 00000001
<<quoted lines omitted: 7>>
> 1000 ==> 11111010000 > 1001 ==> 11111010001
Checking on binary in the REBOL guide, the simpliest way to achieve this seems to be by using enbase and to-binary...
>> to-hex 256
== #00000100
>> enbase/base to-binary [1] 2
== "00000001"
>> enbase/base to-binary [2] 2
== "00000010"
>> enbase/base to-binary [3] 2
== "00000011"
>> enbase/base to-binary [255] 2
== "11111111" A problem arises when you want to go above 1 byte though, as this doesn't work...
>> enbase/base to-binary [256] 2
== "00000000" you having to use two integers in the block to get the desired results...
>> enbase/base to-binary [1 0] 2
== "0000000100000000"
>> enbase/base to-binary [1 1] 2
== "0000000100000001"
>> enbase/base to-binary [255 255] 2
== "1111111111111111" A function using that method might be... base-2: func [n [integer!]][ enbase/base to-binary reduce [to-integer n / 256 n // 256] 2 ] which gives...
>> base-2 0
== "0000000000000000"
>> base-2 1
== "0000000000000001"
>> base-2 2
== "0000000000000010"
>> base-2 3
== "0000000000000011"
>> base-2 255
== "0000000011111111"
>> base-2 256
== "0000000100000000"
>> base-2 257
== "0000000100000001"
>> base-2 65535
== "1111111111111111" Obviously that's restricted to 16 bits and includes them all in the string, but the function could be easily altered to suit your specific needs. HTH. -- Carl Read

 [8/8] from: mh983:attbi at: 10-Sep-2002 18:39


Here's a script I copied from pg 232 of REBOL for Dummies (thanks Ralph Roberts): The commented print is the original which only prints the lowest 8 bits, so I just added "print bin" to print the whole thing: print-binary: func [a [integer! char!]][ bin: enbase/base (load rejoin [ "#{" to-hex either char? a [to-integer a][a] "}"]) 2 ; print find/last/any bin "????????" ; last 8 bits print bin ] sample output:
>> print-binary
23456110 00000001011001011110100101101110

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