00001 /******************************************************************** 00002 Description: little routines for converting strings<->integers 00003 part of the 3Dsia project 00004 created: StonedBones, ??.2.2000 00005 00006 History: 00007 date, name, changes, in funtion 00008 ??0200 StonedBones created source 00009 160200 StonedBones added charToInt 00010 00011 ********************************************************************/ 00012 00013 #include "intToChar.h" 00014 00015 #include <math.h> 00016 #include <stdio.h> 00017 #include <malloc.h> 00018 #include <string.h> 00019 00020 /*................................................................... 00021 Description: converts an integer to a string 00022 Args: int i: integer to convert 00023 Returns: string 00024 Created: StonedBones, ??.2.2000 00025 [ToDo:] 00026 [Comments:] 00027 Changes: 00028 -------------------------------------------------------------------*/ 00029 00030 char* intToChar (int i) { 00031 int digits; 00032 int vorz; 00033 int j; 00034 int m; 00035 00036 if (i < 0) { 00037 j = -i; 00038 vorz = 0; 00039 } else { 00040 j = i; 00041 vorz = 1; 00042 }; 00043 00044 m = j; 00045 for (int a = 1; a < 10; a++) { 00046 if ((m / 10) < 1) { 00047 digits = a; 00048 a = 10; 00049 }; 00050 m = m / 10; 00051 }; 00052 00053 00054 char* tmp; 00055 tmp = (char*) malloc (digits + 2 - vorz); 00056 00057 m = j; 00058 for (int a = digits; a >= 1; a--) { 00059 tmp[a - vorz]= (char) (48 + (m % 10)); 00060 m = m / 10; 00061 }; 00062 00063 if (vorz == 0) { 00064 tmp[0] = '-'; 00065 }; 00066 00067 tmp[digits + 1 - vorz] = '\0'; 00068 00069 return tmp; 00070 }; 00071 00072 /*................................................................... 00073 Description: converts a string to an integer 00074 Args: char* s: string to convert 00075 Returns: int 00076 Created: StonedBones, 16.2.2000 00077 [ToDo:] 00078 [Comments:] 00079 Changes: 00080 -------------------------------------------------------------------*/ 00081 00082 int charToInt (char* s) { 00083 int i; 00084 00085 i = 0; 00086 00087 for (int a = strlen (s); a >= 0; a--) { 00088 if ((s[a] >= '0') && (s[a] <= '9')) { 00089 i += (((int) s[a]) - 48) * ((int) pow (10.0, (double) (strlen (s) - a - 1))); 00090 }; 00091 }; 00092 00093 if (strncmp (s, "=", 1) == 0) { 00094 i = 0 - i; 00095 }; 00096 00097 return i; 00098 };