big_num big_num::operator*(big_num op2) { big_num result = 0; int carry = 0; result.sign = sign*op2.sign; //Optimizing the speed because of the loop's attribute if (number.length() > op2.number.length()) return (op2 * *this); for ( int i = number.length()-1; i >= 0; i--) { big_num temp; for ( int j = op2.number.length()-1; j >= 0; j--) { carry += C2D(number.at(i))*C2D(op2.number.at(j)); temp.number.insert(0,1,D2C(carry%10)); carry /= 10; } while (carry != 0) { temp.number.insert(0,1,D2C(carry%10)); carry /= 10; } temp.number.insert(temp.number.length(), number.length()-i-1, '0'); result = result + temp; } return result; }
/** @brief convert escape characters(%XX) to ASCII character */ void unescape_http_url( char * url /**< pointer to be converted ( escape characters )*/ ) { int x, y; for (x = 0, y = 0; url[y]; ++x, ++y) { if ((url[x] = url[y]) == '%') { url[x] = C2D(url[y+1])*0x10+C2D(url[y+2]); y+=2; } } url[x] = '\0'; }
uint32 ATOI32( char* str, /**< is a pointer to convert */ uint16 base /**< is a base value (must be in the range 2 - 16) */ ) { uint32 num = 0; while (*str !=0) num = num * base + C2D(*str++); return num; }
/** @brief CONVERT STRING INTO INTEGER @return a integer number */ uint16_t ATOI( char *str, /**< is a pointer to convert */ uint16_t base /**< is a base value (must be in the range 2 - 16) */ ) { unsigned int num = 0; while (*str != 0) num = num * base + C2D(*str++); return num; }
/** @brief CONVERT STRING INTO INTEGER @return a integer number */ uint16_t ATOI( uint8_t * str, /**< is a pointer to convert */ uint8_t base /**< is a base value (must be in the range 2 - 16) */ ) { unsigned int num = 0; // debug_2013_11_25 // while (*str !=0) while ((*str !=0) && (*str != 0x20)) // not include the space(0x020) num = num * base + C2D(*str++); return num; }
/** @brief CONVERT STRING INTO HEX OR DECIMAL @return success - 1, fail - 0 */ int ValidATOI( char* str, /**< is a pointer to string to be converted */ int base, /**< is a base value (must be in the range 2 - 16) */ int* ret /**< is a integer pointer to return */ ) { int c; char* tstr = str; if(str == 0 || *str == '\0') return 0; while(*tstr != '\0') { c = C2D(*tstr); if( c >= 0 && c < base) tstr++; else return 0; } *ret = ATOI(str,base); return 1; }
big_num big_num::operator+(big_num op2) { big_num result; //Error handling if (this->sign == 0 || op2.sign == 0) { result.sign = 0; return result; } int index1, index2; int carry = 0; /*CONSIDERING DIFFERENT SITUATION*/ //Compare the two operands' signs switch (sign * op2.sign) { case 1: //=>Same: if (sign == -1) //If both are negative, make the result also negative result.sign = -1; break; case -1: //=>Different: if (sign == -1) //Change the sum operation to substraction operation return (op2 - *this*(-1)); else return (*this - op2*(-1)); break; default: break; } /*DOING THE ADDITION*/ index1 = number.length() - 1; index2 = op2.number.length() - 1; /*Add digit by digit until there's no digit left in one of the two operands*/ for ( ;index1 != -1 && index2 != -1; index1--, index2--) { carry += C2D(number.at(index1)) + C2D(op2.number.at(index2)); result.number.insert(0,1,D2C(carry%10)); carry /= 10; } /*Carry the digits left in the other operand to the result*/ while (index1 >= 0) { if (C2D(number.at(index1))+carry >= 10) { result.number.insert(0,1,D2C((C2D(number.at(index1))+carry)%10)); carry = 1; } else { result.number.insert(0,1,D2C(C2D(number.at(index1)+carry))); carry = 0; } index1--; } while (index2 >= 0) { if (C2D(op2.number.at(index2))+carry >= 10) { result.number.insert(0,1,D2C((C2D(op2.number.at(index2))+carry)%10)); carry = 1; } else { result.number.insert(0,1,D2C(C2D(op2.number.at(index2)+carry))); carry = 0; } index2--; } //Last check the carry var if (carry != 0) result.number.insert(0,1,D2C(carry)); return result; }