as3corelib Tutorial:How to Use IntUtil Class in Flex

Movie-256x256 Int util class contains reusable methods for operations pertaining to int values. It provides convenient ways to rotate int values and to get string of int value in Hex format with specified endian code.

“rol” and “ror” are similar to “>>” and”<<” operators, except that these two methods are circular, with the bits shifted out one end returning on the other end. These methods can be to the left or right.

Search-256x256 Demo | DownloadDownload Full Project

Screenshot:

 IntUtil-01

Enter int value into the input box below to change the int value used to test the methods.

IntUtil-02

IntUtil-03

Enter int value to change the rotate count.

IntUtil-04

IntUtil-05

Maybe you need know something about big-endian and little-endian first , I got these information below from Wikipedia (http://en.wikipedia.org/wiki/Endianness).

big-endian:

With 8-bit atomic element size and 1-byte (octet) address increment:

increasing addresses  →

…

0×0A

0×0B

0×0C

0×0D

…

The most significant byte (MSB) value, which is 0×0A in our example, is stored at the memory location with the lowest address, the next byte value in significance, 0×0B, is stored at the following memory location and so on. This is akin to Left-to-Right reading order in hexadecimal.

With 16-bit atomic element size:

increasing addresses  →

…

0×0A0B

0×0C0D

…

The most significant atomic element stores now the value 0×0A0B, followed by 0×0C0D.

little-endian

With 8-bit atomic element size and 1-byte (octet) address increment:

increasing addresses  →

…

0×0D

0×0C

0×0B

0×0A

…

The least significant byte (LSB) value, 0×0D, is at the lowest address. The other bytes follow in increasing order of significance.

With 16-bit atomic element size:

increasing addresses  →

…

0×0C0D

0×0A0B

…

The least significant 16-bit unit stores the value 0×0C0D, immediately followed by 0×0A0B.

Because the int type is designed as 32-bit in Action Script mode, so I guess the hex value “0×12345678” coded as little-endian should be “0×78563412” coded as big-endian. The value “0×80000000” should be “0×00000080”. However, I found this has been specified in Flex Help document (http://livedocs.adobe.com/flex/201/langref/flash/utils/Endian.html).

The hexadecimal number 0×12345678 has 4 bytes (2 hexadecimal digits per byte).

……

A stream using the bigEndian byte order (the most significant byte first) writes:

12 34 56 78

……

A stream using the littleEndian byte order (the least significant byte first) writes:

78 56 34 12

Methods:

1. rol()

public static function rol(x:int, n:int):int

Rotates x left n bits

Parameters:

x:int

n:int

Returns:

int

2. rol()

public static function rol(x:int, n:int):int

Rotates x right n bits

Parameters:

x:int

n:int

Returns:

Int

3. toHex()

public static function toHex(n:int, bigEndian:Boolean = false):String

Outputs the hex value of a int, allowing the developer to specify the endinaness in the process. Hex output is lowercase.

Parameters:

n:int The int value to output as hex

bigEndian:Boolean (default = false)

Flag to output the int as big or little endian.

Returns:

String A string of length 8 corresponding to the hex representation of n ( minus the leading "0x" )

The following is full source code of IntUtilDemo.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
	xmlns:mx="http://www.adobe.com/2006/mxml"
	layout="absolute"
	backgroundColor="#ffffff">
	<mx:Script>
		<![CDATA[
			import com.adobe.utils.IntUtil;

		]]>
	</mx:Script>
	<mx:VBox width="100%" height="100%">
		<mx:HBox>
			<mx:Label text="original:"/>
			<mx:TextInput id="intValue" text="128"/>
		</mx:HBox>

		<mx:Text text="hex:{ int(intValue.text).toString(16) }"/>
		<mx:Text text="oct:{ int(intValue.text).toString(8) }"/>
		<mx:Text text="bin:{ int(intValue.text).toString(2) }"/>	

		<mx:HRule width="100%" height="1"/>

		<mx:HBox>
			<mx:Label text="rotate count:"/>
			<mx:TextInput id="rotateCount" text="5"/>
		</mx:HBox>

		<mx:HRule width="100%" height="1"/>

		<mx:Text text="Rotates {intValue.text} left {rotateCount.text} bits :{ IntUtil.rol(int(intValue.text),int(rotateCount.text))}"/>
		<mx:Text text="hex:{ IntUtil.rol(int(intValue.text),int(rotateCount.text)).toString(16)}"/>
		<mx:Text text="dec:{ IntUtil.rol(int(intValue.text),int(rotateCount.text)).toString(10)}"/>
		<mx:Text text="oct:{ IntUtil.rol(int(intValue.text),int(rotateCount.text)).toString(8)}"/>
		<mx:Text text="bin:{ IntUtil.rol(int(intValue.text),int(rotateCount.text)).toString(2)}"/>

		<mx:HRule width="100%" height="1"/>

		<mx:Text text="Rotates {intValue.text} right {rotateCount.text} bits :{ IntUtil.ror(int(intValue.text),int(rotateCount.text))}"/>
		<mx:Text text="hex:{ IntUtil.ror(int(intValue.text),int(rotateCount.text)).toString(16)}"/>
		<mx:Text text="dec:{ IntUtil.ror(int(intValue.text),int(rotateCount.text)).toString(10)}"/>
		<mx:Text text="oct:{ IntUtil.ror(int(intValue.text),int(rotateCount.text)).toString(8)}"/>
		<mx:Text text="bin:{ IntUtil.ror(int(intValue.text),int(rotateCount.text)).toString(2)}"/>

		<mx:HRule width="100%" height="1"/>

		<mx:Text text="toHex(littleEndian):{IntUtil.toHex(int(intValue.text))}"/>
		<mx:Text text="toHex(bigEndian):{IntUtil.toHex(int(intValue.text),true)}"/>

		<mx:HRule width="100%" height="1"/>
	</mx:VBox>
</mx:Application>
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

One Response to “as3corelib Tutorial:How to Use IntUtil Class in Flex”

  1. [...] Here is the summary of the Package com.adobe.utils, include ArrayUtil, DirectoryUtil, DateUtil, IntUtil, NumberFormatter, StringUtil and XMLUtil [...]

Leave a Reply