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
Byte Chunks
The byte chunk type extends SenseTalk's chunk expressions to provide all of the flexibility offered by chunk expressions to working with binary data. The byte chunk type can be used to access a single byte or a range of bytes within a data value:
put <010203040506> into myData
put byte 2 of myData --> <02>
put bytes 3 to 4 of myData --> <0304>
put the last 3 bytes of myData --> <040506>
As with other chunk types, a byte chunk is a container, so it can be used to change the data:
put <010203040506> into myData -- <010203040506>
put <AABB> into bytes 2 to 5 of myData -- <01AABB06>
put <77> after byte 2 of myData -- <01AA77BB06>
delete the first 2 bytes of myData -- <77BB06>
Syntax:
byte byteNumber of dataSource
bytes firstByte to lastByte of dataSource
A byte chunk expression is always treated as data in its immediate context (so there is no need to specify as data with it). The dataSource doesn't need to be specified as data. A non-data value will be converted to data automatically before the requested bytes are extracted.