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.
Screenshot:
Enter int value into the input box below to change the int value used to test the methods.
Enter int value to change the rotate count.
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).
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>

September 5th, 2008
Ntt.cc 








Posted in
Tags: 
RSS Feed
Email Feed
[...] Here is the summary of the Package com.adobe.utils, include ArrayUtil, DirectoryUtil, DateUtil, IntUtil, NumberFormatter, StringUtil and XMLUtil [...]