REBOL 3 Docs | Guide | Concepts | Functions | Datatypes | Errors |
TOC < Back Next > | Updated: 30-Apr-2010 Edit History |
The binary! datatype is a series of bytes (8-bits each, octets).
Binary is the raw storage format for all files and networking. It holds encoded data such as images, sounds, strings (in formats like UTF-8 and others), movies, compressed data, encrypted data, and others.
The meaning of an specific binary value depends on what it holds. For example, if you read a JPEG image, it's just a sequence of bytes. Once you've decoded those bytes, it becomes an image! datatype.
The source format for binary data can be base-2 (binary), base-16 (hex), and base-64. The default base for binary data in REBOL is base-16.
Binary strings are written as a number sign (#) followed by a string enclosed in braces. The characters within the string are encoded in one of several formats as specified by an optional number prior to the number sign. Base-16 is the default format.
#{3A18427F 899AEFD8} ; default base-16 2#{10010110110010101001011011001011} ; base-2 64#{LmNvbSA8yw9CB0aGvXmgUkVCu2Uz934b} ; base-64
Spaces, tabs and newlines are permitted within the string. Binary data can span multiple lines.
probe #{
3A
18
92
56
}
#{3A189256}
Strings should contain the correct number of characters to create a binary result which is an integral number of bytes (integral multiple of 8 bits).
The to-binary function converts data to the binary! datatype at the default base set in system/options/binary-base:
probe to-binary "123"
#{313233}
probe to-binary "today is the day..."
#{746F64617920697320746865206461792E2E2E}
To convert an integer into its binary value, pass it in a block:
probe to-binary [1]
#{01}
probe to-binary [11]
#{0B}
Converting a series of integers into a binary, returns the bit conversion for each integer concatenated into a single binary value:
probe to-binary [1 1 1 1]
#{01010101}
Use binary? determine whether a value is an binary! datatype.
probe binary? #{616263}
true
Binary values are a type of series:
probe series? #{616263}
true
probe length? #{616263} ; three hex values in this binary
3
Closely related to working with binary! datatypes are the functions enbase and debase. The enbase function converts strings to their base-2, base-16 or base-64 representations as strings. The debase function converts enbased strings to a binary value of the base specified in system/options/binary-base.
TOC < Back Next > | REBOL.com - WIP Wiki | Feedback Admin |