字符串操作案例

1.拼接字符串

static char SEND_S[13] = {'A','T','+','C','I','P','S','E','N','D','=','1',','};
static char SEND_X[23] = {',','"','1','9','2','.','1','6','8','.','1','0','.','1','"',',','8','8','8','9','
','
'};
static char COMMAND[99]; 

memset( COMMAND, 0, sizeof( COMMAND) );
strcat( COMMAND, SEND_S);
strcat( COMMAND, SEND_X);

2.对比字符串

if( strstr(_udp_rece,"WIFI DISCONNECT") != 0 )  //wifi断开
{

}

3.字符串转整型

int char_int( const char *cha )  //字符串转整型
{
  u8 data[8]={0,0,0,0,0,0,0,0};
  int temp = 0,ret = 0;
  u8 num = 0,err = 0,i = 0,j=0; //位数  正负
  while( *cha )
  {
    if( ( *cha >= '0') && ( *cha <= '9' ) )
    {
      data[num] = (*cha - '0');
      num++;
    }
    if( *cha == '-' )  //负号
    {
      err = 1;
    }
    cha++;
  }
  for( j = 0; j < num; j++  )
  {
    temp = data[j];
    for( i = num - j - 1; i>0 ; i-- )
    {
      temp *= 10;
    }
    ret += temp;
  }
  if( err == 1 )
  {
    ret = -ret;
  }
  return ret;
}

4.整型转字符串

void itoa (int n,char *s)   //整型转字符串
{
  char temp[10];
  int i = 1,j = 0,sign = 0;
  memset( temp, 0 ,10 );
  
  if( n < 0 )//记录符号
  {
    n=-n;//使n成为正数
    sign = -1;
  }
  do
  {
    temp[i++]=n%10+'0';//取下一个数字
  }
  while ((n/=10)>0);//删除该数字
  if(sign<0)
  {
    temp[i++]='-';
  }
  temp[0]='';
  for(j=i-1;j>=0;j--)//生成的数字是逆序的,所以要逆序输出
  {
    *s = temp[j];
    s++;
  }
}

5.取出指定的数据:

字符串数据为:mid:xxx,x:xxx,y:xxx,z:xxx,yaw:xxx,h:xxx中间数据位数不固定
static void req_tello(const char *req)
{
  u8 step = 0,i=0;
  u16 j = 0;
  char *mid = "mid:";
  char *yaw = ";yaw:";
  char *h = ";h:";
  char *temp;
  j = _rece_num;
  while(j--)
  {
    switch ( step )
    {
    case 0: //取出mid数据
      temp = strstr( req,mid);
      while( *temp != ':' )
      {
        temp++;
      }
      while(1)
      {
        temp++;
        if( *temp == ';' ) //跳出
        {
          i = 0;
          step = 1;
          break;
        }
        else  
        {
          requ_tello[step][i] = *temp;
          i++;
        }
      }
      break;
    case 1:  //取出x轴坐标数据
      while( *temp != ':' )
      {
        temp++;
      }
      while(1)
      {
        temp++;
        if( *temp == ';' ) //跳出
        {
          i = 0;
          step = 2;
          break;
        }
        else  
        {
          requ_tello[step][i] = *temp;
          i++;
        }
      }
      break;
    case 2:   //取出y轴坐标数据
      while( *temp != ':' )
      {
        temp++;
      }
      while(1)
      {
        temp++;
        if( *temp == ';' ) //跳出
        {
          i = 0;
          step = 3;
          break;
        }
        else  
        {
          requ_tello[step][i] = *temp;
          i++;
        }
      }
      break;
    case 3:  //取出z轴坐标数据
      while( *temp != ':' )
      {
        temp++;
      }
      while(1)
      {
        temp++;
        if( *temp == ';' ) //跳出
        {
          i = 0;
          step = 4;
          break;
        }
        else  
        {
          requ_tello[step][i] = *temp;
          i++;
        }
      }
      break;
    case 4:  //取出航偏角数据
      temp = strstr( temp,yaw);
      while( *temp != ':' )
      {
        temp++;
      }
      while(1)
      {
        temp++;
        if( *temp == ';' ) //跳出
        {
          i = 0;
          step = 5;
          break;
        }
        else  
        {
          requ_tello[step][i] = *temp;
          i++;
        }
      }
      break;
    case 5:  //取出高度数据
      temp = strstr( temp,h);
      while( *temp != ':' )
      {
        temp++;
      }
      while(1)
      {
        temp++;
        if( *temp == ';' ) //跳出
        {
          i = 0;
          step = 6;
          break;
        }
        else  
        {
          requ_tello[step][i] = *temp;
          i++;
        }
      }
      break;
    case 6:   //将取出的字符串数据转换为整型
      for( i=0; i<6; i++ )
      {
        tello_data[i] = char_int( &requ_tello[i][0] );
      }
      _send_data[0] = 0x6A;
      _send_data[1] = 0x6A;      
      _send_data[2] = BYTE1( tello_data[0] );
      _send_data[3] = BYTE0( tello_data[0] );
      _send_data[4] = BYTE1( tello_data[1] );
      _send_data[5] = BYTE0( tello_data[1] );
      _send_data[6] = BYTE1( tello_data[2] );
      _send_data[7] = BYTE0( tello_data[2] );
      _send_data[8] = BYTE1( tello_data[3] );
      _send_data[9] = BYTE0( tello_data[3] );
      _send_data[10] = BYTE1( tello_data[4] );
      _send_data[11] = BYTE0( tello_data[4] );
      _send_data[12] = BYTE1( tello_data[5] );
      _send_data[13] = BYTE0( tello_data[5] );
      
      memset(requ_tello,0,sizeof(requ_tello));
      _sendflag = 1;
      step = 7;
      break;
    }
    if( step == 7 )
    {
      break;
    }
  }

}

6.利用空闲中断接收不固定长度的数据

stm32f103系列:
void USART1_Configuration(void)       //串口1配置---M
{
  DMA_InitTypeDef DMA_InitStructure;
  USART_InitTypeDef USART_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  //  DMA_InitTypeDef DMA_InitStructure; 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&( USART1->DR);
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)_udp_rece;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = RESUM;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;   //HalfWord
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular ;      // DMA_Mode_Normal  
  DMA_InitStructure.DMA_Priority =  DMA_Priority_High; //DMA_Priority_Low  DMA_Priority_Medium  DMA_Priority_High
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel5, &DMA_InitStructure);
  
  //NVIC 设置,使能中断
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);                    //选择中断分组1  
  
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;             //选择串口3中断
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;      //抢占式中断优先级设置为1
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;             //响应式中断优先级设置为1
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                //使能中断
  NVIC_Init(&NVIC_InitStructure);
  
  USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//开启中断
  
  //USART1
  //TX
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  //RX
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure); 
  
  USART_OverSampling8Cmd(USART1, ENABLE);  
  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
  USART_Init(USART1, &USART_InitStructure); 
  
  USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
  DMA_Cmd(DMA1_Channel5, ENABLE);
  USART_Cmd(USART1, ENABLE); 
  USART1->SR;
  USART1->DR;
}
void USART1_IRQHandler(void) { flagus1 = USART1->SR; if( USART_GetFlagStatus( USART1, USART_FLAG_IDLE ) == SET ) //发生空闲中断 { DMA_Cmd(DMA1_Channel5, DISABLE); //关闭DMA输出 _rece_num = RESUM - DMA1_Channel5 ->CNDTR; //获取读到的字节数 for( u8 i = 0; i < _rece_num; i++ ) { _rece_data[i] = _udp_rece[i]; } //处理数据 DMA1_Channel5->CNDTR = RESUM; //重新填充 DMA_Cmd(DMA1_Channel5, ENABLE); //开启DMA传输 USART_ClearITPendingBit( USART1, USART_IT_IDLE ); //清除中断 } USART1->SR; USART1->DR; }
原文地址:https://www.cnblogs.com/penuel/p/11265582.html