root/as3/Utils/src/org/libspark/utils/BaseUtil.as

リビジョン 739, 4.2 kB (コミッタ: keno, コミット時期: 4 年 前)

汎用的な基数変換を行うクラス(BaseUtil?.as)追加しました

Line 
1 /*
2  * Utils for ActionScript 3.0
3  *
4  * Licensed under the MIT License
5  *
6  * Copyright (c) 2008 Spark project  (www.libspark.org)
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  *
26  */
27 package org.libspark.utils
28 {
29         import flash.utils.Dictionary;
30        
31         /**
32         * 基数変換を行うクラスです。
33         */
34         public class BaseUtil
35         {
36                 /**
37                  * 文字列を数値に変換します。
38                  * numberが"7F"で、dictionaryが"0123456789ABCDEF"(デフォルト)の場合、127を返します。
39                  * @author      Kenichi Ueno
40                  * @param       number: 数値に変換したい文字列(正の数のみ)
41                  * @param       dictionary: 辞書になる文字列("."は小数点として扱うので使えません)
42                  * @return      numberのあらわす数をNumber型で返します。
43                  *
44                  */
45                 static public function StringToNumber(number:String, dictionary:String = "0123456789ABCDEF"):Number
46                 {
47                         var _dictionary:Dictionary = new Dictionary(); // 連想配列
48                         var _base:Number = dictionary.length;
49                         var _numSize:int;
50                         var _i:int;
51                         var _ret:Number = 0;
52                         var _point:int = number.indexOf(".");
53                         number = number.replace(".", "");
54                         _numSize = number.length;
55                         for ( _i = 0; _i < _base; _i++ )
56                         {
57                                 _dictionary[dictionary.charCodeAt(_i)] = _i;
58                         }
59                         for ( _i = 0; _i < _numSize; _i++ )
60                         {
61                                 _ret *= _base;
62                                 _ret += _dictionary[number.charCodeAt(_i)];
63                         }
64                         if ( _point > 0 )
65                         {
66                                 _ret /= Math.pow( _base, _numSize - _point );
67                         }
68                         return _ret;
69                 }
70                 /**
71                  * 数値を文字列に変換します。
72                  * numberが127で、dictionaryが"0123456789ABCDEF"(デフォルト)の場合、"7F"を返します。
73                  * @author      Kenichi Ueno
74                  * @param       number: 数値に変換したい文字列
75                  * @param       dictionary: 辞書になる文字列("."は小数点として扱うので使えません)
76                  * @param       maxDisplayDigitNumber: 小数点以下の最大桁数
77                  * @return      numberをdictionaryを基底として表した文字列を返します。
78                  */
79                 static public function NumberToString(number:Number, dictionary:String = "0123456789ABCDEF", maxDisplayUnderPoint:int = 10):String
80                 {
81                         var _base:Number = dictionary.length;
82                         var _divideNum:Number;
83                         var _dictionary:Array = new Array(_base);
84                         var _i:int;
85                         var _ret:String = "";
86                         var _digitNumber:Number;
87                         var _tempNum:Number;
88                         var _digitCount:int = maxDisplayUnderPoint;
89                         for ( _i = 0; _i < _base; _i++ )
90                         {
91                                 _dictionary[_i] = dictionary.charCodeAt(_i);
92                         }
93                         if ( number == 0 )
94                         {
95                                 return String.fromCharCode(_dictionary[0]);
96                         } else {
97                                 _digitNumber = Math.floor( Math.log(number) / Math.log(_base) ); // 最大桁数
98                         }
99                         _divideNum = Math.pow( _base, _digitNumber );
100                         if ( _digitNumber < 0 )
101                         {
102                                 _ret += String.fromCharCode(_dictionary[0]);
103                         }
104                         while ( (number > 0) && (maxDisplayUnderPoint != (++_digitCount) ) )
105                         {
106                                 if ( _digitNumber-- == -1 )
107                                 {
108                                         _ret += ".";
109                                         _digitCount = 0;
110                                 }
111                                 _tempNum = Math.floor(number / _divideNum);
112                                 _ret += String.fromCharCode(_dictionary[_tempNum]);
113                                 number %= _divideNum;
114                                 number *= _base;
115                         }
116                         while ( _digitNumber-- >= 0 )
117                         {
118                                 _ret += String.fromCharCode(_dictionary[0]);
119                         }
120                         return _ret;
121                 }
122         }
123        
124 }
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。