Beispiel #1
0
/**
 *  \brief      myatoi
 *              Custom function to convert ASCII
 *              string into integer.
 *
 *  \param[in]  char *str, int base, int *result
 *  
 *  \return     RET_CODE
 */
RET_CODE myatoi(char *str, int base, int *result)
{
    int iVal = mystrlen(str) - 1;
    *result = 0;

    if(base == HEX)
    {
        while(*str != EOS)
        {
            if(!ISVALIDHEX(*str)&& !ISVALIDDEC(*str))
            {
                return PARAM_ERR;
            }

            *result += (INTVAL(*str) * mypower(HEX, iVal));
            iVal--;
            str++;
        }
    }
    else if(base == DEC)
    {
        while(*str != EOS)
        {
            if(!ISVALIDDEC(*str))
            {
                return PARAM_ERR;
            }

            *result += (INTVAL(*str) * mypower(DEC, iVal));
            iVal--;
            str++;
        }
    }
    else
    {
        return BASE_ERR;
    }

    return SUCCESS;
}
uint32_t Str2Int(uint8_t *inputstr, int32_t *intnum)
{
  uint32_t i = 0, res = 0;
  uint32_t val = 0;

  if (inputstr[0] == '0' && (inputstr[1] == 'x' || inputstr[1] == 'X'))
  {
    if (inputstr[2] == '\0')
    {
      return 0;
    }
    for (i = 2; i < 11; i++)
    {
      if (inputstr[i] == '\0')
      {
        *intnum = val;
        /* return 1; */
        res = 1;
        break;
      }
      if (ISVALIDHEX(inputstr[i]))
      {
        val = (val << 4) + CONVERTHEX(inputstr[i]);
      }
      else
      {
        /* Return 0, Invalid input */
        res = 0;
        break;
      }
    }
    /* Over 8 digit hex --invalid */
    if (i >= 11)
    {
      res = 0;
    }
  }
  else /* max 10-digit decimal input */
  {
    for (i = 0;i < 11;i++)
    {
      if (inputstr[i] == '\0')
      {
        *intnum = val;
        /* return 1 */
        res = 1;
        break;
      }
      else if ((inputstr[i] == 'k' || inputstr[i] == 'K') && (i > 0))
      {
        val = val << 10;
        *intnum = val;
        res = 1;
        break;
      }
      else if ((inputstr[i] == 'm' || inputstr[i] == 'M') && (i > 0))
      {
        val = val << 20;
        *intnum = val;
        res = 1;
        break;
      }
      else if (ISVALIDDEC(inputstr[i]))
      {
        val = val * 10 + CONVERTDEC(inputstr[i]);
      }
      else
      {
        /* return 0, Invalid input */
        res = 0;
        break;
      }
    }
    /* Over 10 digit decimal --invalid */
    if (i >= 11)
    {
      res = 0;
    }
  }

  return res;
}
Beispiel #3
0
/**
  * @brief  Convert a string to an integer
  * @param  p_inputstr: The string to be converted
  * @param  p_intnum: The integer value
  * @retval 1: Correct
  *         0: Error
  */
uint32_t Str2Int(uint8_t *p_inputstr, uint32_t *p_intnum)
{
  uint32_t i = 0, res = 0;
  uint32_t val = 0;

  if ((p_inputstr[0] == '0') && ((p_inputstr[1] == 'x') || (p_inputstr[1] == 'X')))
  {
    i = 2;
    while ( ( i < 11 ) && ( p_inputstr[i] != '\0' ) )
    {
      if (ISVALIDHEX(p_inputstr[i]))
      {
        val = (val << 4) + CONVERTHEX(p_inputstr[i]);
      }
      else
      {
        /* Return 0, Invalid input */
        res = 0;
        break;
      }
      i++;
    }

    /* valid result */
    if (p_inputstr[i] == '\0')
    {
      *p_intnum = val;
      res = 1;
    }
  }
  else /* max 10-digit decimal input */
  {
    while ( ( i < 11 ) && ( res != 1 ) )
    {
      if (p_inputstr[i] == '\0')
      {
        *p_intnum = val;
        /* return 1 */
        res = 1;
      }
      else if (((p_inputstr[i] == 'k') || (p_inputstr[i] == 'K')) && (i > 0))
      {
        val = val << 10;
        *p_intnum = val;
        res = 1;
      }
      else if (((p_inputstr[i] == 'm') || (p_inputstr[i] == 'M')) && (i > 0))
      {
        val = val << 20;
        *p_intnum = val;
        res = 1;
      }
      else if (ISVALIDDEC(p_inputstr[i]))
      {
        val = val * 10 + CONVERTDEC(p_inputstr[i]);
      }
      else
      {
        /* return 0, Invalid input */
        res = 0;
        break;
      }
      i++;
    }
  }

  return res;
}
Beispiel #4
0
/*******************************************************************************
* Function Name  : Str2Int
* Description    : Convert a string to an integer
* Input 1        : - inputstr: The string to be converted
*                  - intnum: The intger value
* Output         : None
* Return         : 1: Correct
*                  0: Error
*******************************************************************************/
u32 Str2Int(u8 *inputstr, s32 *intnum)
{
  u32 i = 0, res = 0;
  u32 val = 0;

  if (inputstr[0] == '0' && (inputstr[1] == 'x' || inputstr[1] == 'X')){
    if (inputstr[2] == '\0'){//数据包为0 
      return 0;
    }
    for (i = 2; i < 11; i++){//为什么是11?
      if (inputstr[i] == '\0'){//若数据包第一个数字为0,数据大小为0
        *intnum = val;
        /* return 1; */
        res = 1; 
        break;
      }
      if (ISVALIDHEX(inputstr[i])){//filesize中的数据是否有效(A-F || a-f || 0-9),十六进制只能到F      
        val = (val << 4) + CONVERTHEX(inputstr[i]);//若值为“0”-”9“则变化成0-9,否则需要判定是否为A-F,
																									 //若为“A”-“F”,则变化成A-F,否则转化为a-f
      }else{   //若标识size的数值无效(4uk)
        /* return 0, Invalid input */
        res = 0;
        break;
      }
    }
    /* over 8 digit hex --invalid */
    if (i >= 11){
      res = 0; 
    }
  }
  else /* max 10-digit decimal input ,filesize数组不是以0x开头*/
  {
    for (i = 0;i < 11;i++)//由于filesize数组的最大值为11,(该程序中是16)
    {
      if (inputstr[i] == '\0'){//文件为0,直接返回1     
        *intnum = val;
        /* return 1 */
        res = 1;
        break;
      }else if ((inputstr[i] == 'k' || inputstr[i] == 'K') && (i > 0)){ //文件表示为1k,
																																			//直接将size赋值为0x400(1024)(<<10)
        val = val << 10;//(1024 - 1k)
        *intnum = val;
        res = 1;
        break;
      }else if ((inputstr[i] == 'm' || inputstr[i] == 'M') && (i > 0)){//文件大小为M,表示1M,
																																			//直接将size赋值为0x100000 (<<20)
        val = val << 20;
        *intnum = val;
        res = 1;
        break;
      }
#ifdef SecureCRT
      else if (ISVALIDDEC(inputstr[i])){//判定数字是否属于“0“-”9“。按照十进制组合各个位的数值
				val = val * 10 + CONVERTDEC(inputstr[i]);
			}
#else            //使用android方式发送,
			else if (ISVALIDHEX(inputstr[i])){//filesize中的数据是否有效(A-F || a-f || 0-9),十六进制只能到F
        val = (val << 4) + CONVERTHEX(inputstr[i]);//若值为“0”-”9“则变化成0-9,否则需要判定是否为A-F,
																									 //若为“A”-“F”,则变化成A-F,否则转化为a-f
      }
#endif
      else{  
        /* return 0, Invalid input */
        res = 0;
        break;
      }
    }
    /* Over 10 digit decimal --inval	id */
    if (i >= 11){
      res = 0;
    }  
  }
  return (res);
}
Beispiel #5
0
/*----------------------------------------------------------------------
 * Translate a key to an index.
 */
static int translateKey(char key)
{
    if (ISVALIDHEX(key))
        return(CONVERTHEX(key));
    else return -1;
}
Beispiel #6
0
/*******************************************************************************
* Function Name  : Str2Int
* Description    : Convert a string to an integer
* Input 1        : - inputstr: The string to be converted
*                  - intnum: The intger value
* Output         : None
* Return         : 1: Correct
*                  0: Error
*******************************************************************************/
u8 Str2Int(char *inputstr, u32 *intnum)
{
    u8 i = 0, res = 0;
    u32 val = 0;
    if (inputstr[0] == '0' && (inputstr[1] == 'x' || inputstr[1] == 'X'))
    {
        if (inputstr[2] == '\0')
        {
            return 0;
        }
        for (i = 2; i < 11; i++)
        {
            if (inputstr[i] == '\0')
            {
                *intnum = val;
                res = 1; /* return 1; */
                break;
            }
            if (ISVALIDHEX(inputstr[i]))
            {
                val = (val << 4) + CONVERTHEX(inputstr[i]);
            }
            else
            {
                /* return 0; Invalid input */
                res = 0;
                break;
            }
        }
        if (i >= 11) res = 0; /* over 8 digit hex --invalid */
    }
    else /* max 10-digit decimal input */
    {
        for (i = 0;i < 11;i++)
        {
            if (inputstr[i] == '\0')
            {
                *intnum = val;
                /* return 1; */
                res = 1;
                break;
            }
            else if ((inputstr[i] == 'k' || inputstr[i] == 'K') && (i > 0))
            {
                val = val << 10;
                *intnum = val;
                res = 1;
                break;
            }
            else if ((inputstr[i] == 'm' || inputstr[i] == 'M') && (i > 0))
            {
                val = val << 20;
                *intnum = val;
                res = 1;
                break;
            }
            else if (ISVALIDDEC(inputstr[i]))
                val = val * 10 + CONVERTDEC(inputstr[i]);
            else
            {
                /* return 0; Invalid input */
                res = 0;
                break;
            }
        }
        if (i >= 11) res = 0; /* Over 10 digit decimal --invalid */
    }
    return res;
}