Binary Data Manipulation with SenseTalk
Most SenseTalk scripts work with data in the form of text and numbers, and sometimes other types of values such as dates or colors. When needed, SenseTalk can also deal with data in its binary form (the raw bits and bytes that are stored on a computer).
Data Values
Raw data can be represented directly in a script using a pair of hexadecimal digits for each byte of the data, enclosed in angle brackets, < and >.
put <00> into nullByte // a single byte, with a value of zero
put <48656c6c6f> into secretMessage // five bytes of data
The put before
and put after
forms of the put
command can be used to insert additional binary data before or after an existing value.
put <20467269 656e6421> after secretMessage // append more data
When two known binary data values are compared for equality, they are compared byte for byte to see that they have exactly the same binary contents.
put secretMessage is <48656c6c6f20467269656e6421> --> True
Syntax:
< hexadecimalData >
The hexadecimalData must consist of an even number of hexadecimal digits 0 through 9 and A through F. Spaces may be used to break the sequence up for readability.
AsData
Function, As Data
Operator
Behavior: The asData
function, most often called using the as data
operator, converts any value to its binary representation.
Use the asData
function or as data
operator when you want to tell SenseTalk to treat a value as binary data. This is especially useful for reading or writing a file or URL in its raw binary form (as described later in this chapter), but can also be used at any time to work with or display a value in its binary form.
When two known binary data values are compared for equality, they are compared byte for byte to see that they have exactly the same binary contents. Use as data
to ensure that such a binary comparison is made.
Syntax:
asData( aValue )
aValue as data
The as data
operator is usually more readable and natural to use than the asData
function, but is otherwise identical in functionally.
Examples:
put "abcdefg" as data --> <61626364 656667>
put file "picture.jpg" as data into rawImageData // read file contents as data
if file "monet.png" as data is equal to oldData as data then
// Do something
end if