Usage of bytearray class

Source: Internet
Author: User

Use of flash. utils. bytearray class

The bytearray class provides methods and attributes for optimizing reading, writing, and processing binary data.

Bytearray stores byte streams.

Therefore, the stored content is stored in byte format.

Boolean Type, occupies, one byte, eight digits

Double type, which occupies 8 bytes, 8*8 = 64 bits.

Fill in 0 with a few digits, for example, 3.1415926, convert it into a binary form and store it in its array, and then convert it into a byte by 8 digits, occupies a position in bytearray (Note ),

For example:

Bytearr. writedouble (math. Pi );

Trace (bytearr [1]); // 64
Trace (bytearr [2]); // 9
Trace (bytearr [3]); // 33
Trace (bytearr [4]); // 251
Trace (bytearr [5]); // 84
Trace (bytearr [6]); // 68
Trace (bytearr [7]); // 45
Trace (bytearr [8]); // 24

In the output result, for example, 64, which is the 8-bit hexadecimal

Below is a simple example of this class in the API:

package {    import flash.display.Sprite;    import flash.utils.ByteArray;    import flash.errors.EOFError;    public class ByteArrayExample extends Sprite {                public function ByteArrayExample() {            var byteArr:ByteArray = new ByteArray();            byteArr.writeBoolean(false);            trace(byteArr.length);            // 1            trace(byteArr[0]);            // 0            byteArr.writeDouble(Math.PI);            trace(byteArr.length);            // 9            trace(byteArr[0]);            // 0            trace(byteArr[1]);            // 64            trace(byteArr[2]);            // 9            trace(byteArr[3]);            // 33            trace(byteArr[4]);            // 251            trace(byteArr[5]);            // 84            trace(byteArr[6]);            // 68            trace(byteArr[7]);            // 45            trace(byteArr[8]);            // 24                        byteArr.position = 0;            try {                trace(byteArr.readBoolean() == false); // true            }             catch(e:EOFError) {                trace(e);           // EOFError: Error #2030: End of file was encountered.            }                        try {                trace(byteArr.readDouble());        // 3.141592653589793            }            catch(e:EOFError) {                trace(e);           // EOFError: Error #2030: End of file was encountered.            }                        try {                trace(byteArr.readDouble());            }             catch(e:EOFError) {                trace(e);            // EOFError: Error #2030: End of file was encountered.            }        }    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.