IntUtil.as 4.4 KB

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