Last change
on this file since 681 was 563, checked in by OverTaker, 17 years ago |
とりあえず必要な分だけ。続きはビット演算が好きな方にお願いしたいです。
|
File size:
2.3 KB
|
Line | |
---|
1 |
|
---|
2 |
|
---|
3 | Namespace System
|
---|
4 |
|
---|
5 | /*!
|
---|
6 | @brief 基本型の変換を行うクラス
|
---|
7 | */
|
---|
8 |
|
---|
9 | Class Convert
|
---|
10 | Public
|
---|
11 | /*!
|
---|
12 | @brief 文字列を32ビット符号付き値に変換する
|
---|
13 | @author OverTaker
|
---|
14 | @param 文字列
|
---|
15 | @return 32ビット符号付き値
|
---|
16 | */
|
---|
17 | Static Function ToInt32(value As String) As Long
|
---|
18 | Return ToInt32(value, 10)
|
---|
19 | End Function
|
---|
20 |
|
---|
21 | /*!
|
---|
22 | @brief 文字列を任意の進数で32ビット符号付き値に変換する
|
---|
23 | @author OverTaker
|
---|
24 | @param 文字列
|
---|
25 | @param 基数
|
---|
26 | @return 32ビット符号付き値
|
---|
27 | */
|
---|
28 | Static Function ToInt32(value As String, fromBase As Long) As Long
|
---|
29 | Dim retVal As Long
|
---|
30 | Dim negative = False As Boolean
|
---|
31 |
|
---|
32 | Dim init = 0 As Long
|
---|
33 | While ActiveBasic.CType.IsSpace(value[init])
|
---|
34 | init++
|
---|
35 | Wend
|
---|
36 |
|
---|
37 | If Detail.IsMinus(value[init]) Then
|
---|
38 | init++
|
---|
39 | negative = True
|
---|
40 | End If
|
---|
41 |
|
---|
42 | Dim i As Long
|
---|
43 | Dim n As Long
|
---|
44 | For i = init To ELM(value.Length)
|
---|
45 | n = Detail.NumberFromChar(value[i])
|
---|
46 | If n < fromBase Then
|
---|
47 | retVal += n * fromBase^(ELM(value.Length) - i)
|
---|
48 | Else
|
---|
49 | Throw New ArgumentOutOfRangeException("System.Convert.ToInt32: The value was out of basevalue.", "value or fromBase")
|
---|
50 | End If
|
---|
51 | Next
|
---|
52 |
|
---|
53 | If negative Then
|
---|
54 | Return -retVal
|
---|
55 | Else
|
---|
56 | Return retVal
|
---|
57 | End If
|
---|
58 | End Function
|
---|
59 | End Class
|
---|
60 |
|
---|
61 | Namespace Detail
|
---|
62 | /*!
|
---|
63 | @brief 文字を値に変換する
|
---|
64 | @author OverTaker
|
---|
65 | @param 文字
|
---|
66 | @return 値
|
---|
67 | */
|
---|
68 | Function NumberFromChar(aCharacter As WCHAR) As Long
|
---|
69 | If ActiveBasic.CType.IsDigit(aCharacter) Then
|
---|
70 | Return aCharacter - &H30
|
---|
71 | ElseIf ActiveBasic.CType.IsUpper(aCharacter) Then
|
---|
72 | Return aCharacter - &H37
|
---|
73 | ElseIf ActiveBasic.CType.IsLower(aCharacter) Then
|
---|
74 | Return aCharacter - &H57
|
---|
75 | Else
|
---|
76 | Throw New ArgumentException("System.Convert.Detail: The character was invalid.", "aCharacter")
|
---|
77 | End If
|
---|
78 | End Function
|
---|
79 |
|
---|
80 | /*!
|
---|
81 | @overload
|
---|
82 | */
|
---|
83 | Function NumberFromChar(aCharacter As CHAR) As Long
|
---|
84 | Return NumberFromChar(ActiveBasic.CType.Detail.Widen(aCharacter))
|
---|
85 | End Function
|
---|
86 |
|
---|
87 | /*!
|
---|
88 | @brief 文字が-記号かどうかを調べる
|
---|
89 | @author OverTaker
|
---|
90 | @param 文字
|
---|
91 | @retval True 文字は-
|
---|
92 | @retval Flase 文字は-でない
|
---|
93 | */
|
---|
94 | Function IsMinus(aCharacter As WCHAR) As Boolean
|
---|
95 | Return aCharacter = &H2D
|
---|
96 | End Function
|
---|
97 |
|
---|
98 | /*!
|
---|
99 | @overload
|
---|
100 | */
|
---|
101 | Function IsMinus(aCharacter As CHAR) As Boolean
|
---|
102 | Return IsMinus(ActiveBasic.CType.Detail.Widen(aCharacter))
|
---|
103 | End Function
|
---|
104 | End Namespace
|
---|
105 |
|
---|
106 | End Namespace
|
---|
Note:
See
TracBrowser
for help on using the repository browser.