| 1 |
|
|---|
| 2 | package com.adobe.utils {
|
|---|
| 3 |
|
|---|
| 4 | import flash.utils.Endian;
|
|---|
| 5 |
|
|---|
| 6 | /**
|
|---|
| 7 | * Contains reusable methods for operations pertaining
|
|---|
| 8 | * to int values.
|
|---|
| 9 | */
|
|---|
| 10 | public class IntUtil {
|
|---|
| 11 |
|
|---|
| 12 | /**
|
|---|
| 13 | * Rotates x left n bits
|
|---|
| 14 | *
|
|---|
| 15 | * @langversion ActionScript 3.0
|
|---|
| 16 | * @playerversion Flash 9.0
|
|---|
| 17 | * @tiptext
|
|---|
| 18 | */
|
|---|
| 19 | public static function rol ( x:int, n:int ):int {
|
|---|
| 20 | return ( x << n ) | ( x >>> ( 32 - n ) );
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | /**
|
|---|
| 24 | * Rotates x right n bits
|
|---|
| 25 | *
|
|---|
| 26 | * @langversion ActionScript 3.0
|
|---|
| 27 | * @playerversion Flash 9.0
|
|---|
| 28 | * @tiptext
|
|---|
| 29 | */
|
|---|
| 30 | public static function ror ( x:int, n:int ):uint {
|
|---|
| 31 | var nn:int = 32 - n;
|
|---|
| 32 | return ( x << nn ) | ( x >>> ( 32 - nn ) );
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | /** String for quick lookup of a hex character based on index */
|
|---|
| 36 | private static var hexChars:String = "0123456789abcdef";
|
|---|
| 37 |
|
|---|
| 38 | /**
|
|---|
| 39 | * Outputs the hex value of a int, allowing the developer to specify
|
|---|
| 40 | * the endinaness in the process. Hex output is lowercase.
|
|---|
| 41 | *
|
|---|
| 42 | * @param n The int value to output as hex
|
|---|
| 43 | * @param bigEndian Flag to output the int as big or little endian
|
|---|
| 44 | * @return A string of length 8 corresponding to the
|
|---|
| 45 | * hex representation of n ( minus the leading "0x" )
|
|---|
| 46 | * @langversion ActionScript 3.0
|
|---|
| 47 | * @playerversion Flash 9.0
|
|---|
| 48 | * @tiptext
|
|---|
| 49 | */
|
|---|
| 50 | public static function toHex( n:int, bigEndian:Boolean = false ):String {
|
|---|
| 51 | var s:String = "";
|
|---|
| 52 |
|
|---|
| 53 | if ( bigEndian ) {
|
|---|
| 54 | for ( var i:int = 0; i < 4; i++ ) {
|
|---|
| 55 | s += hexChars.charAt( ( n >> ( ( 3 - i ) * 8 + 4 ) ) & 0xF )
|
|---|
| 56 | + hexChars.charAt( ( n >> ( ( 3 - i ) * 8 ) ) & 0xF );
|
|---|
| 57 | }
|
|---|
| 58 | } else {
|
|---|
| 59 | for ( var x:int = 0; x < 4; x++ ) {
|
|---|
| 60 | s += hexChars.charAt( ( n >> ( x * 8 + 4 ) ) & 0xF )
|
|---|
| 61 | + hexChars.charAt( ( n >> ( x * 8 ) ) & 0xF );
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | return s;
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | }
|
|---|