- 积分
- 136119
- 注册时间
- 2014-12-27
- 最后登录
- 2024-3-28
- 在线时间
- 602 小时
- 威望
- 562
- 贡献
- 29
- 金币
- 52623
- 钢镚
- 1422
- 交易凭证
- 1
- 分享
- 0
- 精华
- 33
- 帖子
- 2094
- 主题
- 1742
TA的每日心情 | 擦汗 2018-4-10 15:18 |
---|
签到天数: 447 天 [LV.9]以坛为家II
超级版主
- 威望
- 562
- 贡献
- 29
- 金币
- 52623
- 钢镚
- 1422
|
- package cn.util
- {
- import flash.utils.ByteArray;
- /**
- * 此类搜集了一些算法源码
- * @author 破晓
- *
- */
- public class Utils
- {
- public function Utils()
- {
- }
-
- /**
- * 编码转换
- * @param str
- * @param fromChar
- * @param toChar
- * @return
- *
- */
- public static function stringTransform(str:String, fromChar:String, toChar:String):String
- {
- var byte:ByteArray = new ByteArray();
- byte.writeMultiByte(str, fromChar);
- byte.position = 0;
-
- return byte.readMultiByte(byte.length, toChar);
- }
-
- /**
- * 编码转换
- * @param str
- * @param toChar
- * @return
- *
- */
- public static function stringEncode(str:String, toChar:String):ByteArray
- {
- var byte:ByteArray = new ByteArray();
- byte.writeMultiByte(str, toChar);
- byte.position = 0;
-
- return byte;
- }
-
- /**
- * 编码转换
- * @param strSource
- * @param fromChar
- * @param toChar
- * @return
- *
- */
- public static function stringSourceEncode(strSource:ByteArray, fromChar:String, toChar:String):ByteArray
- {
- var str:String = strSource.readMultiByte(strSource.bytesAvailable, fromChar);
- var byte:ByteArray = new ByteArray();
- byte.writeMultiByte(str, toChar);
- byte.position = 0;
-
- return byte;
- }
-
- //****************************************************************
- // 随机数生成算法
- //****************************************************************
-
- /**随机数种子*/
- private static const MAXRATIO:Number=1/(uint.MAX_VALUE+1);
- private static var _seed:uint;
- /**
- * 产生随机数
- * @return 随机数
- *
- */
- public static function rand():Number
- {
- _seed^=(_seed<<21);
- _seed^=(_seed>>>35);
- _seed^=(_seed<<4);
-
- return _seed*MAXRATIO;
- }
- //****************************************************************
- // 位运算技巧
- //****************************************************************
-
- /**
- * 将 base 乘以 2 的 pow 次方
- * @param base
- * @param pow
- * @return 将 base 乘以 2 的 pow 次方的结果
- *
- */
- public static function getPow2(base:int, pow:int):int
- {
- if(pow > 0)
- return base << pow;
- else
- return base >> pow;
- }
-
- /**
- * 将数字转换为整数
- * @param num
- * @return 整数
- *
- */
- public static function numberToInteger(num:Number):int
- {
- return num >> 0;
- }
-
- public static const RGB_TYPE:int = 24;
- public static const ARGB_TYPE:int = 32;
- /**
- * 提取颜色组成成分
- * @param color
- * @param type
- * @return {r, g, b} 或 {a, r, g, b}
- *
- */
- public static function getColorInfo(color:uint, type:int=RGB_TYPE):Object
- {
- var obj:Object = {};
- if(type == RGB_TYPE)
- {
- obj.r = color >> 16;
- obj.g = color >> 8 & 0xFF;
- obj.b = color & 0xFF;
- }
- else if(type == ARGB_TYPE)
- {
- obj.a = color >>> 24;
- obj.r = color >>> 16 & 0xFF;
- obj.g = color >>> 8 & 0xFF;
- obj.b = color & 0xFF;
- }
-
- return obj;
- }
-
- /**
- * 合成颜色
- * @param r
- * @param g
- * @param b
- * @param a
- * @param type
- * @return 颜色
- *
- */
- public static function buildColor(r:uint, g:uint, b:uint, a:uint=0, type:int=RGB_TYPE):uint
- {
- if(type == RGB_TYPE)
- return r << 16 | g << 8 | b;
- else
- return a << 24 | r << 16 | g << 8 | b;
- }
-
- /**
- * 交换两个整数的值
- * @param a
- * @param b
- * @return {a, b}
- *
- */
- public static function exchange(a:int, b:int):Object
- {
- a ^= b;
- b ^= a;
- a ^= b;
-
- return {a:a, b:b};
- }
-
- /**
- * 自增(i++)
- * @param value
- * @return
- *
- */
- public static function increment(value:int):int
- {
- return -~value;
- }
-
- /**
- * 自减(i--)
- * @param value
- * @return
- *
- */
- public static function decrement(value:int):int
- {
- return ~-value;
- }
-
- /**
- * 返回相反数
- * @param value
- * @return 相反数
- *
- */
- public static function flipping(value:int):int
- {
- return ~value + 1; // 或 (value ^ -1) + 1
- }
-
- /**
- * 判断是否为奇数
- * @param value
- * @return
- *
- */
- public static function isEven(value:int):Boolean
- {
- return Boolean(value & 1);
- }
-
- /**
- * 返回绝对值
- * @param value
- * @return 绝对值
- *
- */
- public static function abs(value:int):int
- {
- return (value ^ (value >> 31)) - (value >> 31);
- }
-
- /**
- * 判断两个整数符号是否相同
- * @param a
- * @param b
- * @return
- *
- */
- public static function comparingSign(a:int, b:int):Boolean
- {
- return a ^ b >= 0;
- }
- }
- }
复制代码
|
|