mas01mj@732
|
1 /*
|
mas01mj@732
|
2 Copyright (c) 2008, Adobe Systems Incorporated
|
mas01mj@732
|
3 All rights reserved.
|
mas01mj@732
|
4
|
mas01mj@732
|
5 Redistribution and use in source and binary forms, with or without
|
mas01mj@732
|
6 modification, are permitted provided that the following conditions are
|
mas01mj@732
|
7 met:
|
mas01mj@732
|
8
|
mas01mj@732
|
9 * Redistributions of source code must retain the above copyright notice,
|
mas01mj@732
|
10 this list of conditions and the following disclaimer.
|
mas01mj@732
|
11
|
mas01mj@732
|
12 * Redistributions in binary form must reproduce the above copyright
|
mas01mj@732
|
13 notice, this list of conditions and the following disclaimer in the
|
mas01mj@732
|
14 documentation and/or other materials provided with the distribution.
|
mas01mj@732
|
15
|
mas01mj@732
|
16 * Neither the name of Adobe Systems Incorporated nor the names of its
|
mas01mj@732
|
17 contributors may be used to endorse or promote products derived from
|
mas01mj@732
|
18 this software without specific prior written permission.
|
mas01mj@732
|
19
|
mas01mj@732
|
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
mas01mj@732
|
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
mas01mj@732
|
22 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
mas01mj@732
|
23 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
mas01mj@732
|
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
mas01mj@732
|
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
mas01mj@732
|
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
mas01mj@732
|
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
mas01mj@732
|
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
mas01mj@732
|
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
mas01mj@732
|
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
mas01mj@732
|
31 */
|
mas01mj@732
|
32 package com.adobe.utils {
|
mas01mj@732
|
33
|
mas01mj@732
|
34 import flash.utils.Endian;
|
mas01mj@732
|
35
|
mas01mj@732
|
36 /**
|
mas01mj@732
|
37 * Contains reusable methods for operations pertaining
|
mas01mj@732
|
38 * to int values.
|
mas01mj@732
|
39 */
|
mas01mj@732
|
40 public class IntUtil {
|
mas01mj@732
|
41
|
mas01mj@732
|
42 /**
|
mas01mj@732
|
43 * Rotates x left n bits
|
mas01mj@732
|
44 *
|
mas01mj@732
|
45 * @langversion ActionScript 3.0
|
mas01mj@732
|
46 * @playerversion Flash 9.0
|
mas01mj@732
|
47 * @tiptext
|
mas01mj@732
|
48 */
|
mas01mj@732
|
49 public static function rol ( x:int, n:int ):int {
|
mas01mj@732
|
50 return ( x << n ) | ( x >>> ( 32 - n ) );
|
mas01mj@732
|
51 }
|
mas01mj@732
|
52
|
mas01mj@732
|
53 /**
|
mas01mj@732
|
54 * Rotates x right n bits
|
mas01mj@732
|
55 *
|
mas01mj@732
|
56 * @langversion ActionScript 3.0
|
mas01mj@732
|
57 * @playerversion Flash 9.0
|
mas01mj@732
|
58 * @tiptext
|
mas01mj@732
|
59 */
|
mas01mj@732
|
60 public static function ror ( x:int, n:int ):uint {
|
mas01mj@732
|
61 var nn:int = 32 - n;
|
mas01mj@732
|
62 return ( x << nn ) | ( x >>> ( 32 - nn ) );
|
mas01mj@732
|
63 }
|
mas01mj@732
|
64
|
mas01mj@732
|
65 /** String for quick lookup of a hex character based on index */
|
mas01mj@732
|
66 private static var hexChars:String = "0123456789abcdef";
|
mas01mj@732
|
67
|
mas01mj@732
|
68 /**
|
mas01mj@732
|
69 * Outputs the hex value of a int, allowing the developer to specify
|
mas01mj@732
|
70 * the endinaness in the process. Hex output is lowercase.
|
mas01mj@732
|
71 *
|
mas01mj@732
|
72 * @param n The int value to output as hex
|
mas01mj@732
|
73 * @param bigEndian Flag to output the int as big or little endian
|
mas01mj@732
|
74 * @return A string of length 8 corresponding to the
|
mas01mj@732
|
75 * hex representation of n ( minus the leading "0x" )
|
mas01mj@732
|
76 * @langversion ActionScript 3.0
|
mas01mj@732
|
77 * @playerversion Flash 9.0
|
mas01mj@732
|
78 * @tiptext
|
mas01mj@732
|
79 */
|
mas01mj@732
|
80 public static function toHex( n:int, bigEndian:Boolean = false ):String {
|
mas01mj@732
|
81 var s:String = "";
|
mas01mj@732
|
82
|
mas01mj@732
|
83 if ( bigEndian ) {
|
mas01mj@732
|
84 for ( var i:int = 0; i < 4; i++ ) {
|
mas01mj@732
|
85 s += hexChars.charAt( ( n >> ( ( 3 - i ) * 8 + 4 ) ) & 0xF )
|
mas01mj@732
|
86 + hexChars.charAt( ( n >> ( ( 3 - i ) * 8 ) ) & 0xF );
|
mas01mj@732
|
87 }
|
mas01mj@732
|
88 } else {
|
mas01mj@732
|
89 for ( var x:int = 0; x < 4; x++ ) {
|
mas01mj@732
|
90 s += hexChars.charAt( ( n >> ( x * 8 + 4 ) ) & 0xF )
|
mas01mj@732
|
91 + hexChars.charAt( ( n >> ( x * 8 ) ) & 0xF );
|
mas01mj@732
|
92 }
|
mas01mj@732
|
93 }
|
mas01mj@732
|
94
|
mas01mj@732
|
95 return s;
|
mas01mj@732
|
96 }
|
mas01mj@732
|
97 }
|
mas01mj@732
|
98
|
mas01mj@732
|
99 } |