|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.Text; |
|
19 using System.IO; |
|
20 using System.Collections; |
|
21 |
|
22 namespace SymbianUtils |
|
23 { |
|
24 public class NumberBaseUtils |
|
25 { |
|
26 #region Enumerations |
|
27 public enum TNumberBase |
|
28 { |
|
29 EDecimal = 10, |
|
30 EHex = 16 |
|
31 } |
|
32 #endregion |
|
33 |
|
34 public static long TextToDecimalNumber( string aText ) |
|
35 { |
|
36 string copyOfText = aText; |
|
37 TNumberBase numberBase = TNumberBase.EDecimal; |
|
38 long longResult = 0; |
|
39 // |
|
40 bool result = TextToDecimalNumber( ref copyOfText, out longResult, out numberBase ); |
|
41 if ( result == false ) |
|
42 { |
|
43 longResult = 0; |
|
44 } |
|
45 // |
|
46 return longResult; |
|
47 } |
|
48 |
|
49 public static long TextToDecimalNumber( string aText, TNumberBase aNumberBase ) |
|
50 { |
|
51 long ret = 0; |
|
52 // |
|
53 try |
|
54 { |
|
55 ret = System.Convert.ToInt64( aText, (int) aNumberBase ); |
|
56 } |
|
57 finally |
|
58 { |
|
59 } |
|
60 // |
|
61 return ret; |
|
62 } |
|
63 |
|
64 public static bool TextToDecimalNumber( ref string aText, out long aValue, out TNumberBase aBase ) |
|
65 { |
|
66 string address = aText; |
|
67 |
|
68 // Check if its a decimal or hex string |
|
69 aBase = TNumberBase.EDecimal; |
|
70 aValue = 0; |
|
71 // |
|
72 TNumberBase numberBase = TNumberBase.EDecimal; |
|
73 if (address.Length > 2 && (address.Substring(0, 2) == "0x" || address.Substring(0, 2) == "0X")) |
|
74 { |
|
75 // Assume hex & remove prefix |
|
76 numberBase = TNumberBase.EHex; |
|
77 address = address.Substring(2); |
|
78 } |
|
79 |
|
80 // Check each char |
|
81 bool okToConvert = (address.Length > 0); |
|
82 foreach (char character in address) |
|
83 { |
|
84 if (character != ' ' && char.IsDigit(character) == false) |
|
85 { |
|
86 // Is it a hex digit? |
|
87 char upperCaseChar = char.ToUpper(character); |
|
88 switch (upperCaseChar) |
|
89 { |
|
90 case 'A': |
|
91 case 'B': |
|
92 case 'C': |
|
93 case 'D': |
|
94 case 'E': |
|
95 case 'F': |
|
96 numberBase = TNumberBase.EHex; // Now its definite |
|
97 break; |
|
98 default: |
|
99 okToConvert = false; |
|
100 break; |
|
101 } |
|
102 } |
|
103 } |
|
104 |
|
105 address = address.Trim(); |
|
106 if ( address.Length > 0 && okToConvert ) |
|
107 { |
|
108 // Convert number to base 10. |
|
109 try |
|
110 { |
|
111 aValue = System.Convert.ToInt64( address, (int) numberBase ); |
|
112 aText = address; |
|
113 aBase = numberBase; |
|
114 } |
|
115 catch( ArgumentOutOfRangeException ) |
|
116 { |
|
117 okToConvert = false; |
|
118 } |
|
119 } |
|
120 return okToConvert; |
|
121 } |
|
122 |
|
123 public static bool TryTextToDecimalNumber( ref string aText, out string aOutput, out long aValue, out TNumberBase aBase ) |
|
124 { |
|
125 string address = aText; |
|
126 int endingOffset = 0; |
|
127 |
|
128 // Check if its a decimal or hex string |
|
129 aBase = TNumberBase.EDecimal; |
|
130 aValue = 0; |
|
131 // |
|
132 TNumberBase numberBase = TNumberBase.EDecimal; |
|
133 int characterIndex = 0; |
|
134 string prefix = string.Empty; |
|
135 if (address.Length > 2 && (address.Substring(0, 2) == "0x" || address.Substring(0, 2) == "0X")) |
|
136 { |
|
137 // Assume hex & remove prefix |
|
138 numberBase = TNumberBase.EHex; |
|
139 prefix = address.Substring(0, 2); |
|
140 address = address.Substring(2); |
|
141 } |
|
142 |
|
143 // Check each char |
|
144 bool validCharacter = (address.Length > 0); |
|
145 while (validCharacter && characterIndex < address.Length ) |
|
146 { |
|
147 char character = address[characterIndex]; |
|
148 if (char.IsDigit(character) == false) |
|
149 { |
|
150 // Is it a hex digit? |
|
151 char upperCaseChar = char.ToUpper(character); |
|
152 switch (upperCaseChar) |
|
153 { |
|
154 case '-': |
|
155 break; |
|
156 case 'A': |
|
157 case 'B': |
|
158 case 'C': |
|
159 case 'D': |
|
160 case 'E': |
|
161 case 'F': |
|
162 numberBase = TNumberBase.EHex; // Now its definite |
|
163 break; |
|
164 default: |
|
165 validCharacter = false; |
|
166 break; |
|
167 } |
|
168 } |
|
169 |
|
170 characterIndex++; |
|
171 if (validCharacter) |
|
172 endingOffset++; |
|
173 } |
|
174 |
|
175 if (endingOffset > 0) |
|
176 { |
|
177 // Convert number to base 10. |
|
178 aOutput = address.Substring(0, endingOffset).Trim(); |
|
179 aValue = System.Convert.ToInt64(aOutput, (int) numberBase); |
|
180 aOutput = prefix + aOutput; |
|
181 aBase = numberBase; |
|
182 aText = aText.Substring(endingOffset + prefix.Length); |
|
183 } |
|
184 else |
|
185 { |
|
186 aOutput = string.Empty; |
|
187 aValue = 0; |
|
188 aBase = TNumberBase.EDecimal; |
|
189 } |
|
190 |
|
191 return (endingOffset > 0); |
|
192 } |
|
193 |
|
194 public static bool TryTextToDecimalNumber( ref string aText, out string aOutput, out long aValue, TNumberBase aBase ) |
|
195 { |
|
196 string address = aText; |
|
197 int endingOffset = 0; |
|
198 |
|
199 // Check if its a decimal or hex string |
|
200 aValue = 0; |
|
201 // |
|
202 TNumberBase numberBase = aBase; |
|
203 int characterIndex = 0; |
|
204 string prefix = string.Empty; |
|
205 if (address.Length > 2 && (address.Substring(0, 2) == "0x" || address.Substring(0, 2) == "0X")) |
|
206 { |
|
207 // Assume hex & remove prefix |
|
208 numberBase = TNumberBase.EHex; |
|
209 prefix = address.Substring(0, 2); |
|
210 address = address.Substring(2); |
|
211 } |
|
212 |
|
213 // Check each char |
|
214 bool validCharacter = (address.Length > 0); |
|
215 while( validCharacter && characterIndex < address.Length ) |
|
216 { |
|
217 char character = address[characterIndex]; |
|
218 if (char.IsDigit(character) == false) |
|
219 { |
|
220 // Is it a hex digit? |
|
221 char upperCaseChar = char.ToUpper(character); |
|
222 switch (upperCaseChar) |
|
223 { |
|
224 case '-': |
|
225 break; |
|
226 case 'A': |
|
227 case 'B': |
|
228 case 'C': |
|
229 case 'D': |
|
230 case 'E': |
|
231 case 'F': |
|
232 numberBase = TNumberBase.EHex; // Now its definite |
|
233 break; |
|
234 default: |
|
235 validCharacter = false; |
|
236 break; |
|
237 } |
|
238 } |
|
239 |
|
240 characterIndex++; |
|
241 if (validCharacter) |
|
242 endingOffset++; |
|
243 } |
|
244 |
|
245 if (endingOffset > 0) |
|
246 { |
|
247 try |
|
248 { |
|
249 // Convert number to base 10. |
|
250 string output = address.Substring(0, endingOffset).Trim();; |
|
251 aValue = System.Convert.ToInt64( output, (int) numberBase ); |
|
252 aOutput = prefix + output; |
|
253 aText = aText.Substring(endingOffset + prefix.Length); |
|
254 } |
|
255 catch( Exception ) |
|
256 { |
|
257 endingOffset = -1; |
|
258 aOutput = string.Empty; |
|
259 aValue = 0; |
|
260 } |
|
261 } |
|
262 else |
|
263 { |
|
264 aOutput = string.Empty; |
|
265 aValue = 0; |
|
266 } |
|
267 |
|
268 return (endingOffset > 0); |
|
269 } |
|
270 } |
|
271 } |