Other bases


native! to-hex  Red-by-example    MyCode4fun

Converts an integer! to a hex issue! datatype (with leading # and 0's).


>> to-hex 10
== #0000000A

>> to-hex 16
== #00000010

>> to-hex 15
== #0000000F



/size        => Specify number of hex digits in result.


>> to-hex/size 15 4
== #000F

>> to-hex/size 10 2
== #0A



native!  enbase and native! debase,    Red-by-example    MyCode4fun

These are used do code and decode binary-coded strings.
These are not for number conversion and, honestly, I don't understand the use for them, but here is how they work:

>> enbase "my house"
== "bXkgaG91c2U="

>> probe to-string debase "bXkgaG91c2U="
"my house"
== "my house"

/base        => Binary base to use. It may be 64 (default), 16 or 2.

>> enbase/base "Hi" 2
== "0100100001101001"

>> probe to-string debase/base "0100100001101001" 2
"Hi"
== "Hi"

native! dehex  Red-by-example  

Converts URL-style hex encoded (%xx) strings.

>> dehex "www.mysite.com/this%20is%20my%20page"
== "www.mysite.com/this is my page" ; Hex 20 (%20) is space

>> dehex "%33%44%55"
== "3DU"
; %33 is hex for "3", %44 is hex for "D" and %55 is hex for "U".


Bitwise functions:

Bitwise functions work at the binary level of values:

op! >>   Red-specs  Red-by-example

right shift - documentation says: "lowest bits are shifted out, highest bit is duplicated".


>> 144 >> 2

== 36


I could not figure out how to duplicate the highest bit if it's 1. I tried 32 bit words, but Red converts them to floats.

op! <<   Red-specs  Red-by-example

left shift - highest bits are shifted out, zero bits are added to the right.

>> 17 << 1

== 34


op! >>>  Red-specs  Red-by-example

logical shift -  lowest bits are shifted out, zero bits are added to the left.  I could not figure out how this is different from >>.


op! and  &  and~   Red-specs  Red-by-example

>> 27 and 50

== 18



0

0

0

1

1

0

1

1


27













0

0

1

1

0

0

1

0


50












and

0

0

0

1

0

0

1

0


18


The functional version (not infix) of and is and~


op! or  &  or~   Red-specs  Red-by-example

>> 27 or 50

== 59



0

0

0

1

1

0

1

1


27













0

0

1

1

0

0

1

0


50












or

0

0

1

1

1

0

1

1


59


The functional version (not infix) of or is or~


op! xor   xor~   Red-specs  Red-by-example

>> 27 xor 50

== 41



0

0

0

1

1

0

1

1


27













0

0

1

1

0

0

1

0


50












xor

0

0

1

0

1

0

0

1


41


The functional version (not infix) of xor is xor~


action! complement   Red-specs  Red-by-example

todo -

todo


< Previous topic                                                                                          Next topic >