number_convert.inc
Original include source with line numbers.
| 1 | /* |
| 2 | |
| 3 | // Number Conversions |
| 4 | // by Exolent |
| 5 | |
| 6 | stock HexToDecimal( const szHex[ ] ) |
| 7 | |
| 8 | stock DecimalToHex( const iDecimal, szHexResult[ ], const iHexLen ) |
| 9 | |
| 10 | stock BinaryToDecimal( const szBinary[ ] ) |
| 11 | |
| 12 | stock DecimalToBinary( const iDecimal, szBinary[ ], const iBinaryLen ) |
| 13 | |
| 14 | stock BinaryToHex( const szBinary[ ], szHexResult[ ], const iHexLen ) |
| 15 | |
| 16 | stock HexToBinary( const szHex[ ], szBinaryResult[ ], const iBinaryLen ) |
| 17 | |
| 18 | // Retrives digits from a given value and returns the number of digits |
| 19 | // |
| 20 | // @param iValue - The value from which to get the digits |
| 21 | // @param iDigits[ ] - The array to store the digits in (optional) |
| 22 | // @param iDigitSize - The size of the array (optional) |
| 23 | // @param iBase - The base of the number to find digits by (default is 10 for base 10 (decimal)) (optional) |
| 24 | // @param bKeepDigitOrder - Decides to start from the right side of the decimal or left (default is false) (optional) (see post for more details) |
| 25 | // |
| 26 | // @return The total number of digits found |
| 27 | // |
| 28 | stock GetDigits( const iValue, iDigits[ ] = "", const iDigitSize = 0, const iBase = 10, bool:bKeepDigitOrder = false ) |
| 29 | |
| 30 | stock ReverseArray( iArray[ ], const iSize ) |
| 31 | |
| 32 | */ |
| 33 | |
| 34 | #include < amxmodx > |
| 35 | |
| 36 | #if cellbits == 32 |
| 37 | #define MAX_DECIMAL_DIGITS 10 |
| 38 | #define MAX_BINARY_DIGITS 31 |
| 39 | #define MAX_HEXADECIMAL_DIGITS 8 |
| 40 | #else |
| 41 | #define MAX_DECIMAL_DIGITS 19 |
| 42 | #define MAX_BINARY_DIGITS 63 |
| 43 | #define MAX_HEXADECIMAL_DIGITS 16 |
| 44 | #endif |
| 45 | |
| 46 | stock const __HEXCHARS[ ] = "0123456789ABCDEF"; |
| 47 | |
| 48 | stock HexToDecimal( const szHex[ ] ) |
| 49 | { |
| 50 | new iLen = strlen( szHex ); |
| 51 | new iPos = 0; |
| 52 | new cChar; |
| 53 | new iValue; |
| 54 | new iResult = 0; |
| 55 | |
| 56 | while( --iLen >= 0 ) |
| 57 | { |
| 58 | cChar = szHex[ iLen ]; |
| 59 | |
| 60 | switch( cChar ) |
| 61 | { |
| 62 | case '0' .. '9': |
| 63 | { |
| 64 | iValue = cChar - '0'; |
| 65 | } |
| 66 | case 'a' .. 'f': |
| 67 | { |
| 68 | iValue = 10 + cChar - 'a'; |
| 69 | } |
| 70 | case 'A' .. 'F': |
| 71 | { |
| 72 | iValue = 10 + cChar - 'A'; |
| 73 | } |
| 74 | default: |
| 75 | { |
| 76 | return -1; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | iResult += ( power( 16, iPos++ ) * iValue ); |
| 81 | } |
| 82 | |
| 83 | return iResult; |
| 84 | } |
| 85 | |
| 86 | stock DecimalToHex( const iDecimal, szHexResult[ ], const iHexLen ) |
| 87 | { |
| 88 | new iTotal = GetDigits( iDecimal, szHexResult, iHexLen, 16, true ); |
| 89 | |
| 90 | for( new i = 0; i < iTotal; i++ ) |
| 91 | { |
| 92 | szHexResult[ i ] = __HEXCHARS[ szHexResult[ i ] ]; |
| 93 | } |
| 94 | |
| 95 | szHexResult[ iTotal ] = 0; |
| 96 | |
| 97 | return iTotal; |
| 98 | } |
| 99 | |
| 100 | /*stock BinaryToDecimal( const iBinary ) |
| 101 | { |
| 102 | new iResult = 0; |
| 103 | new iTemp = iBinary; |
| 104 | new iValue; |
| 105 | new iPos; |
| 106 | |
| 107 | while( iTemp > 0 ) |
| 108 | { |
| 109 | iValue = iTemp % 10; |
| 110 | |
| 111 | if( iValue ) |
| 112 | { |
| 113 | iResult |= ( 1 << iPos++ ); |
| 114 | } |
| 115 | |
| 116 | iTemp /= 10; |
| 117 | } |
| 118 | |
| 119 | return iResult; |
| 120 | } |
| 121 | |
| 122 | stock DecimalToBinary( const iDecimal ) |
| 123 | { |
| 124 | new iResult = 0; |
| 125 | |
| 126 | for( new i = MAX_BINARY_DIGITS - 1, iBit = 1 << i; i >= 0; iBit = 1 << ( --i ) ) |
| 127 | { |
| 128 | if( iDecimal & iBit ) |
| 129 | { |
| 130 | iResult += power( 10, i ); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return iResult; |
| 135 | }*/ |
| 136 | |
| 137 | stock BinaryToDecimal( const szBinary[ ] ) |
| 138 | { |
| 139 | new cChar; |
| 140 | new iPos = 0; |
| 141 | new iResult = 0; |
| 142 | |
| 143 | while( ( cChar = szBinary[ iPos ] ) ) |
| 144 | { |
| 145 | if( cChar == '1' ) |
| 146 | { |
| 147 | iResult |= ( 1 << iPos ); |
| 148 | } |
| 149 | else if( cChar != '0' ) |
| 150 | { |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | iPos++; |
| 155 | } |
| 156 | |
| 157 | return iResult; |
| 158 | } |
| 159 | |
| 160 | stock DecimalToBinary( const iDecimal, szBinary[ ], const iBinaryLen ) |
| 161 | { |
| 162 | new iLen = 0; |
| 163 | |
| 164 | for( new i = min( MAX_BINARY_DIGITS - 1, iBinaryLen ), iBit = 1 << i; i >= 0; iBit = 1 << ( --i ) ) |
| 165 | { |
| 166 | if( iDecimal & iBit ) |
| 167 | { |
| 168 | szBinary[ iLen++ ] = '1'; |
| 169 | } |
| 170 | else if( iLen ) |
| 171 | { |
| 172 | szBinary[ iLen++ ] = '0'; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | szBinary[ iLen ] = 0; |
| 177 | |
| 178 | return iLen; |
| 179 | } |
| 180 | |
| 181 | /*stock BinaryToHex( const iBinary, szHexResult[ ], const iHexLen ) |
| 182 | { |
| 183 | new iByteDigits[ MAX_BINARY_DIGITS / 4 ]; |
| 184 | new iTotal = GetDigits( iBinary, iByteDigits, sizeof( iByteDigits ), 10000, true ); |
| 185 | |
| 186 | new iPos; |
| 187 | |
| 188 | for( new i = 0; i < iTotal && iPos < iHexLen; i++ ) |
| 189 | { |
| 190 | iPos += DecimalToHex( BinaryToDecimal( iByteDigits[ i ] ), szHexResult[ iPos ], iHexLen - iPos ); |
| 191 | } |
| 192 | |
| 193 | szHexResult[ iPos ] = 0; |
| 194 | |
| 195 | return iPos; |
| 196 | } |
| 197 | |
| 198 | stock HexToBinary( const szHex[ ] ) |
| 199 | { |
| 200 | new iLen = strlen( szHex ); |
| 201 | new iPos; |
| 202 | new cChar; |
| 203 | new iValue; |
| 204 | new iResult = 0; |
| 205 | |
| 206 | while( --iLen >= 0 ) |
| 207 | { |
| 208 | cChar = szHex[ iLen ]; |
| 209 | |
| 210 | switch( cChar ) |
| 211 | { |
| 212 | case '0' .. '9': |
| 213 | { |
| 214 | iValue = cChar - '0'; |
| 215 | } |
| 216 | case 'a' .. 'f': |
| 217 | { |
| 218 | iValue = 10 + cChar - 'a'; |
| 219 | } |
| 220 | case 'A' .. 'F': |
| 221 | { |
| 222 | iValue = 10 + cChar - 'A'; |
| 223 | } |
| 224 | default: |
| 225 | { |
| 226 | return -1; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | iResult += DecimalToBinary( power( 16, iPos++ ) * iValue ); |
| 231 | } |
| 232 | |
| 233 | return iResult; |
| 234 | }*/ |
| 235 | |
| 236 | stock BinaryToHex( const szBinary[ ], szHexResult[ ], const iHexLen ) |
| 237 | { |
| 238 | new iBinaryLen = strlen( szBinary ); |
| 239 | new szByte[ 5 ], iLen; |
| 240 | new iByteValue; |
| 241 | new iHexPos; |
| 242 | |
| 243 | for( new iPos = iBinaryLen - 1; iPos >= 0 && iHexPos < iHexLen; ) |
| 244 | { |
| 245 | iLen = 0; |
| 246 | |
| 247 | while( iLen < 4 && iPos >= 0 ) |
| 248 | { |
| 249 | szByte[ 4 - ( ++iLen ) ] = szBinary[ iPos-- ]; |
| 250 | } |
| 251 | |
| 252 | copy( szByte, iLen, szByte[ 4 - iLen ] ); |
| 253 | |
| 254 | iByteValue = BinaryStringToDecimal( szByte ); |
| 255 | |
| 256 | szHexResult[ iHexPos++ ] = __HEXCHARS[ iByteValue ]; |
| 257 | } |
| 258 | |
| 259 | ReverseArray( szHexResult, iHexPos ); |
| 260 | |
| 261 | szHexResult[ iHexPos ] = 0; |
| 262 | |
| 263 | return iHexPos; |
| 264 | } |
| 265 | |
| 266 | stock HexToBinary( const szHex[ ], szBinaryResult[ ], const iBinaryLen ) |
| 267 | { |
| 268 | new iHexPos; |
| 269 | new cChar; |
| 270 | new iValue; |
| 271 | new iByteIndex; |
| 272 | new iBinaryPos; |
| 273 | |
| 274 | while( ( cChar = szHex[ iHexPos ] ) && iBinaryPos < iBinaryLen ) |
| 275 | { |
| 276 | switch( cChar ) |
| 277 | { |
| 278 | case '0' .. '9': |
| 279 | { |
| 280 | iValue = cChar - '0'; |
| 281 | } |
| 282 | case 'a' .. 'f': |
| 283 | { |
| 284 | iValue = 10 + cChar - 'a'; |
| 285 | } |
| 286 | case 'A' .. 'F': |
| 287 | { |
| 288 | iValue = 10 + cChar - 'A'; |
| 289 | } |
| 290 | default: |
| 291 | { |
| 292 | return -1; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | for( iByteIndex = 0; iByteIndex < 4 && iBinaryPos < iBinaryLen; iByteIndex++ ) |
| 297 | { |
| 298 | szBinaryResult[ iBinaryPos++ ] = ( iValue & ( 1 << iByteIndex ) ) ? '1' : '0'; |
| 299 | } |
| 300 | |
| 301 | iHexPos++; |
| 302 | } |
| 303 | |
| 304 | szBinaryResult[ iBinaryPos ] = 0; |
| 305 | |
| 306 | return iBinaryPos; |
| 307 | } |
| 308 | |
| 309 | stock GetDigits( const iValue, iDigits[ ] = "", const iDigitSize = 0, const iBase = 10, bool:bKeepDigitOrder = false ) |
| 310 | { |
| 311 | new iDigitCount; |
| 312 | new iTemp = iValue; |
| 313 | |
| 314 | while( iTemp > 0 && iDigitCount < iDigitSize ) |
| 315 | { |
| 316 | iDigits[ iDigitCount++ ] = iTemp % iBase; |
| 317 | iTemp /= iBase; |
| 318 | } |
| 319 | |
| 320 | if( bKeepDigitOrder ) |
| 321 | { |
| 322 | ReverseArray( iDigits, iDigitCount ); |
| 323 | } |
| 324 | |
| 325 | return iDigitCount; |
| 326 | } |
| 327 | |
| 328 | stock ReverseArray( iArray[ ], const iSize ) |
| 329 | { |
| 330 | new iMiddle = iSize / 2; |
| 331 | new iTemp; |
| 332 | |
| 333 | for( new i = 0, iOpposite = iSize - 1; i < iMiddle; i++, iOpposite-- ) |
| 334 | { |
| 335 | iTemp = iArray[ i ]; |
| 336 | iArray[ i ] = iArray[ iOpposite ]; |
| 337 | iArray[ iOpposite ] = iTemp; |
| 338 | } |
| 339 | } |
| 340 | /* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE |
| 341 | *{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang11274\\ f0\\ fs16 \n\\ par } |
| 342 | */ |
| 343 | |