Raised This Month: $51 Target: $400
 12% 

String <-> Int


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
[ --<-@ ] Black Rose
ANNIHILATED
Join Date: Sep 2005
Location: Stockholm, Sweden.
Old 07-06-2009 , 11:07   String <-> Int
Reply With Quote #1

Code:
// Valid radix can be anything from 2 to 36. #define Binary  2 #define Ternary  3 #define Trinary  3 #define Quintal  5 #define Oct   8 #define Octal  8 #define Dec   10 #define Decimal  10 #define Duodecimal 12 #define Dozenal  12 #define Hex   16 #define Hexadecimal 16 new ___key[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" #define ___ctod(%0) ('0' <= %0 <= '9' ? %0 - '0' : 'A' <= %0 <= 'Z' ? %0 -'A' + 10 : 'a' <= %0 <= 'z' ? %0 -'a' + 10 : 0) // Converts an integer to a string. stock itoa(val, out[], len, radix = Decimal) {    if ( ! val ) { out[0] = '0'; out[1] = 0; return; }  if ( ! ( 2 <= radix <= 36 ) ) { out[0] = '0'; out[1] = 0; return; }    for ( new i = len ; i ; i-- ) { out[i] = 0; }    for ( new i = len - 1 ; val && i > -1 ; --i, val /= radix )   out[len-i-1] = ___key[val % radix];    new len2 = strlen(out);  new temp;    for ( new i = 0 ; i < len2 / 2 ; i++ ) {   temp = out[i];   out[i] = out[len2-i-1];   out[len2-i-1] = temp;  } } // Converts a string to an integer. stock atoi(string[], radix = Decimal) {    if ( ! ( 2 <= radix <= 36 ) ) { return 0; }    new result, mult = 1;    for ( new i = strlen(string) - 1 ; i > - 1 ; i-- ) {   result += ___ctod(string[i]) * mult;   mult *= radix  }    return result; }

Last edited by [ --<-@ ] Black Rose; 07-07-2009 at 07:25.
[ --<-@ ] Black Rose is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:48.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode