What's wrong with

>> to-hex 56
== #00000038
>> to-hex 2037
== #000007F5
>> to-hex -56
== #FFFFFFC8
>> to-hex -2037
== #FFFFF80B

Mike.

----- Original Message -----
From: [EMAIL PROTECTED]
Date: Wednesday, October 11, 2000 7:50 am
Subject: [REBOL] Bitwise manipulation Re:

> Hi, Phil...
> 
> [EMAIL PROTECTED] wrote:
> > 
> > An interesting one ...
> > 
> > I have a integer value that I must convert to binary:
> > 
> > Nice and easy for values <= 127
> > 
> > int1: 56
> > 
> > b1: to-binary to-block int1
> > 
> > HOWEVER, if int1 is greater than 127, say 2037, I have to 
> perform the
> > 
> > following task:
> > 
> > 2037 which is equivalent to 0000100001011000 must be split into 
> 2 bytes as
> > 
> > follows
> > 
> > b1: -> 00001000 effectively b1: to-binary to-block 8
> > 
> > b2: -> 01011000 effectively b2: to-binary to-block 24
> > 
> > Any suggestions..
> > 
> 
> Two suggestions:
> 
> 1)  Try using
> 
>        big-endian: func [n [integer!] /local r] [
>            r: copy #{}
>            until [
>                insert r to-binary to-char (n // 256)
>                0 = n: to-integer n / 256
>            ]
>            r
>        ]
> 
>        >> big-endian 56
>        == #{38}
>        >> big-endian 2037
>        == #{07F5}
> 
>    However, *DANGER, WILL ROBINSON!*,
> 
>    1.1) This assumes byte order for values requiring more than 8 
> bits.    1.2) It breaks for negative values!  (Explaining why and 
> proposing         a repair are left as exercises for the reader... 
> ;-)
> 
> 2)  Check your math.  Last time I looked, 2037 was odd, which 
> means that
>    its binary representation MUST end in a one!  ;-)
> 
> -jn-
> 
> -- 
> ; Joel Neely  [EMAIL PROTECTED]  901-263-4460  38017/HKA/9677
> REBOL []  print to-string debase decompress #{
>    789C0BCE0BAB4A7176CA48CAB53448740FABF474F3720BCC
>    B6F4F574CFC888342AC949CE74B50500E1710C0C24000000}
> 
> 

Reply via email to