General:. The
conversion operator ( data %%% code) is used to
convert raw data from one frame size to another. The endian operator
(data >%< bytes)is used to reverse the byte order
of characters in a string according to their frame size in bytes. The examples
below cover all usage of these two operators.
Code 0: Convert to bits:
Code 0 converts the data to bits. The data may be a string or numeric vector.
If numeric, integers are shown as 32 bits and floats (doubles) are shown as 64
bits.
Code 1: Convert to/from (_1)
CHAR; (1) UCHAR: . Code 1 converts the data to 1 byte frames. Strings
are framed into 1 byte CHAR or UCHAR. If a negative 1 (_1) is used
CHAR is assumed and the bytes are converted to integers. If a positive 1
(1) is used the byte is taken as UCHAR and the bytes are converted to
unsigned integers between 0 and 255 (same as #asc). If a numeric
vector is given, its elements are converted to a single byte per numeric
element.
Code 2: Convert to/from (_2)
SHORT; (2) USHORT:. Code 2 converts the data to two byte frames.
Strings are framed into two bytes and the resulting numeric is returned. If a
negative 2, it is taken as SHORT. If a positive 2, it is taken as USHORT. For
numerics, the number is converted to short and then the string of bytes is
returned.
Code 4: Convert to/from (_4) INT;
(4) UINT; (4.) FLOAT:. Code 4 converts the data to four byte frames.
Strings are framed into four bytes and the resulting numeric is returned. If a
negative 4, it is taken as INT. If a positive 4, it is taken as UINT. (Note:
if the sign bit of any element is set, the whole resulting numeric is floated.
This is because Glee has no form for UINT and would treat it as a
negative number...it would take more than 4 bytes to represent it as a positive
number) If the code is a float 4.0, then the 4 byte frames are taken as 4
byte floats and become Glee floats. For numerics with an integer
4 code, the number is converted to integer and then the string of bytes is
returned. The sign of the code is irrelevant as either sign results in the same
internal bytes. For numerics with a floating 4.0 code, the number is converted
to a 4 byte float and those bytes are returned.
Code 8: Convert to/from (8.)
DOUBLE:. Code 8 converts the data to eight byte frames. Strings are
taken as framed in 8 byte doubles and the resulting numeric float is returned.
Eight byte integers is not yet supported (my machine can't do them). For
numerics, they are first converted to 8 byte doubles and then the resulting 8
characters are returned as frames in the resulting string.
Endian ( data >%<
bytes: Swap bytes from big endian to little endian and vice versa:.
Different processors assume different layouts for the bytes making up numerics.
There are two styles. Big endian has largest order bytes first. Little endian
has smallest order bytes first. Intel machines are little endian. When you do
bit twiddling this can be problematic as this example shows. In <1> I
create and save in "b" a 32 bit vector of all zeros. This is the bit
representation of a numeric integer zero. <2> I then set the 10th bit to
true. <3> Next I convert those bits to an integer and display it ... I
get 16384 when I might expect 1024 (i.e. 2^10). <4> I use the
data >%< 4 operation to reverse the endian in a 4 byte
frame and save in "eb" and display. I now see 4194304. <5>
Taking another angle, I convert the number 1024 to bits saving it in
"b". <6> I switch endian on the result in a 4 byte frame and
save it in "eb". I can now see the bits in the customary fashing with
the lest significant bits on the right. <7> I show that indeed, bit 10 is
the one set. <8> If I convert these bits to an integer I get 262144, not
the 1024 I might expect. <9>But if I reverse the endian and then convert
I get the 1024 as I expect.
Being able to manipulate the bits and endian in this way allows
Glee relatively easy and efficient access to any data common
layout.
Endian examples 2, 4, and 8
byte frames: . The previous example worked with integers (4 byte frames).
This example illustrates Glee's ability to manipulate endian on
data in 2, 4 and 8 byte frames.