サイトトップ

Director Flash 書籍 業務内容 プロフィール

Flash ActionScript 3.0 Reference

◎07 ByteArray

☆02 ★バイナリデータに32bit整数を読み書きしたい

解説
32bit整数は、4バイトとして扱われます。その読み書きは、1バイト単位で行う必要はなく、ByteArray.readInt()およびByteArray.writeInt()メソッドで4バイトまとめて処理することができます。なお、ByteArrayクラスによるバイナリデータの扱いおよびその基本的な仕組みについては、前述「バイナリデータをバイト単位で読み書きしたい」をお読みください。

→関連項目
バイナリデータをバイト単位で読み書きしたい

ByteArrayクラス
パッケージ flash.utils
継承 ByteArray → Object
readInt()メソッド ASタイプ M
ランタイムバージョン AIR 1.0/Flash Player 9
文法 readInt():int
意味 ByteArrayインスタンスのデータから4バイトを32bit符号つき整数として読込む。
引数 なし。
戻り値 読込んだ32bit符号つき整数(-2147483648〜2147483647)。
writeInt()メソッド ASタイプ M
ランタイムバージョン AIR 1.0/Flash Player 9
文法 writeInt(value:int):void
意味 ByteArrayインスタンスのデータに4バイトを32bit符号つき整数として書込む。
引数 value:int…32bit符号つき整数として書込む整数値。
戻り値 なし。

記述例
以下のスクリプト07-02-01は、ByteArrayインスタンスのデータに対して、ByteArray.readInt()およびByteArray.writeInt()メソッドにより32bit整数値を読み書きします。1バイト単位のByteArray.readByte()ByteArray.writeByte()メソッドと比べ、4バイトのデータにアクセスしていることをお確かめください。

スクリプト07-02-01■ByteArrayインスタンスのデータに32bit整数値を読み書きする

// フレームアクション
var myByteArray:ByteArray = new ByteArray();
myByteArray.writeByte(0x12);
myByteArray.writeByte(0x34);
myByteArray.writeByte(0x56);
myByteArray.writeByte(0x78);
myByteArray.writeByte(0x76);
myByteArray.position = 0;
var nInt:int = myByteArray.readInt();   // 先頭(0)から4バイト読込み
trace(nInt.toString(16));   // 出力: 12345678
var nPosition:uint = myByteArray.position;
trace(nPosition);   // 出力: 4
myByteArray.writeInt(0x87654321);   // 末尾に4バイト書込み
for (var i:uint = nPosition; i < myByteArray.length; i++) {
  var nByte:int = myByteArray[i];
  trace(i, nByte.toString(16));
}

ByteArray.writeByte()メソッドで5バイト書込まれたデータの先頭からByteArray.readInt()メソッドを呼出すと、最初の4バイトが読込まれます。また、データの末尾で8桁の16進数を渡してByteArray.writeInt()メソッドを呼出せば、4バイトのデータが書込まれます。フレームアクションの[出力]結果は、下図07-02-01のとおりです。

図07-02-01■フレームアクションの[出力]結果 【07_02_ByteArray_read_writeInt_001.png/07_02_ByteArray_read_writeInt_002.png】

※数値を読み書きするメソッド
数値を読み書きするメソッドは、数値の種類に応じて下表07-02-01のように備えられています。

表07-02-01■数値を読み書きするメソッド
数値 データ型 読込み 書込み
8bit整数 符号つき int readByte() writeByte()
符号なし uint readUnsignedByte()
16bit整数 符号つき int readShort() writeShort()
符号なし uint readUnsignedShort()
32bit整数 符号つき int readInt() writeInt()
符号なし uint readUnsignedInt() writeUnsignedInt()
32bit浮動小数点数 Number readFloat() writeFloat()
64bit浮動小数点数 Number readDouble() writeDouble()

作成者: 野中文雄
ドラフト作成: 2009年7月26日


Copyright © 2001-2009 Fumio Nonaka.  All rights reserved.