C语言:使用strstr函数对经纬度进行解析

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

int main()
{
    char *lptr = NULL;
    char *tptr = NULL;
    char tmp[30] = {0};
    char *GBS_USART0_receive_buf = "0,29.489428,106.639084,550,2020/6/17,9:34:16";

    double D_latitude = 0.0;
    double D_longitude = 0.0;

    uint64_t latitude = 0;
    uint64_t longitude = 0;

    uint8_t size = 0;
    uint8_t year = 0;
    uint8_t month = 0;
    uint8_t day = 0;
    uint8_t hour = 0;
    uint8_t minute = 0;
    uint8_t second = 0;

    uint8_t gps_time_arr[8] = {0x00, 0x00};
    uint32_t gps_time = 0;

    lptr=strstr((const char*)GBS_USART0_receive_buf,",");
    if(lptr==NULL) return 0;

    lptr++;
    tptr = strstr((const char*)lptr,",");
    if(tptr==NULL) return 0;

    size = tptr - lptr;
    strncpy(tmp, lptr, size);

    D_latitude = atof(tmp);

	printf("D_latitude = %lf\n", D_latitude);

    lptr = tptr + 1;
    tptr = strstr((const char*)lptr,",");
    if(tptr==NULL) return 0;

    size = tptr - lptr;

    memset(tmp, 0, sizeof(tmp));
    strncpy(tmp, lptr, size);

    D_longitude = atof(tmp);

    printf("D_longitude = %lf\n", D_longitude);

    tptr = lptr + 1;
    lptr=strstr((const char*)tptr,",20");

    lptr = lptr + 3;
    tptr = strstr((const char*)lptr,"/");
    if(tptr==NULL) return 0;
    size = tptr - lptr;
    memset(tmp, 0, sizeof(tmp));
    strncpy(tmp, lptr, size);
    year = atoi(tmp);

    printf("year = %d\n", year);

    lptr = tptr+1;
    tptr = strstr((const char*)lptr,"/");
    if(tptr==NULL) return 0;
    size = tptr - lptr;
    memset(tmp, 0, sizeof(tmp));
    strncpy(tmp, lptr, size);
    month = atoi(tmp);

	printf("month = %d\n", month);

    lptr = tptr + 1;
    tptr = strstr((const char*)lptr,",");
    if(tptr==NULL) return 0;
    size = tptr - lptr;
    memset(tmp, 0, sizeof(tmp));
    strncpy(tmp, lptr, size);
    day = atoi(tmp);

	printf("day = %d\n", day);

    lptr = tptr + 1;
    tptr = strstr((const char*)lptr,":");
    if(tptr==NULL) return 0;
    size = tptr - lptr;
    memset(tmp, 0, sizeof(tmp));
    strncpy(tmp, lptr, size);
    hour = atoi(tmp);

	printf("hour = %d\n", hour);

    lptr = tptr + 1;
    tptr = strstr((const char*)lptr,":");
    if(tptr==NULL) return 0;
    size = tptr - lptr;
    memset(tmp, 0, sizeof(tmp));
    strncpy(tmp, lptr, size);
    minute = atoi(tmp);

	printf("minute = %d\n", minute);

    lptr = tptr + 1;
    memset(tmp, 0, sizeof(tmp));
    strncpy(tmp, lptr, 2);
    second = atoi(tmp);

	printf("second = %d\n", second);

    latitude = D_latitude * 10000000;
    longitude = D_longitude * 10000000;

	printf("latitude = %ld\n", latitude);
	printf("longitude = %ld\n", longitude);


	return 0;
}

程序运行结果: 

D_latitude = 29.489428
D_longitude = 106.639084
year = 20
month = 6
day = 17
hour = 9
minute = 34
second = 16
latitude = 294894280
longitude = 1066390840

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AllenSun-1990

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值