Chapter 34. Working with Binary Data
Introduction
In this chapter
Writing and reading bytes
Using byte arrays
A byte array is an instance of the flash.utils.ByteArray
class. As the name suggests, a byte array allows you to work with binary data by storing it in an array of bytes. A byte is unit of data comprised of 8 bits. A bit is the atomic unit of data used in your computer. A bit can contain only two possible values: 0 and 1. When you put 8 bits together to form a byte, the combined range is from 0 to 255. Although as humans we are more accustomed to working with a decimal counting system, all computer operations take place using binary behind the scenes. This means that a byte array is a convenient way to work with lower‐level binary data for a variety of reasons. Most of the uses of byte arrays in ActionScript are fairly specialized, but an understanding of byte arrays and possible uses will help you to use a byte array appropriately when it would be useful.
Creating a Byte Array
You can create byte arrays in several ways. The most obvious is to use the constructor as follows:
var bytes:ByteArray = new ByteArray();
The other ways to create byte arrays rely on the built‐in ActionScript methods that return new ByteArray objects. For example, the BitmapData
class has a getPixels()
method, which returns a ByteArray
object.
Writing to a Byte Array
You can write data to a byte array in two basic ways: Use array‐access notation ([]
) to write to a specific index, and use the write methods: ...
Get ActionScript™ 3.0 Bible now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.