解析C字符串

发布于 2024-10-31 05:55:26 字数 431 浏览 1 评论 0原文

我对 C 很不熟悉。出现了一个逗号分隔的字符串,我需要理清它。一些位对应于数值,其他位对应于字等。

输入

char str_in;

str_in = "$GPRMC,114353.000,A,6016.3245,N,02458.3270,E,0.01,0,A*69";

输出

#include <string.h>
float lat, lon, time;

time = 114353.000;
lat = 60+(1/60)*16.3245; //Conversion to decimal degrees
lon = 024+(1/60)*58.3270;

所有间距保持不变。我一直在努力解决的部分是从纬度/经度中提取前两位/三位数字并以不同的方式对待它们。有人可以帮忙吗?

I'm quite unfamiliar with C. A comma sepearted string comes in and I need to untangle it. Some bits correspond to numerical values, others to words etc.

Input

char str_in;

str_in = "$GPRMC,114353.000,A,6016.3245,N,02458.3270,E,0.01,0,A*69";

Output

#include <string.h>
float lat, lon, time;

time = 114353.000;
lat = 60+(1/60)*16.3245; //Conversion to decimal degrees
lon = 024+(1/60)*58.3270;

All the spacings remain unchanged. The section I have struggled with is extracting the first two/three digits from the latitude/longitude and treating them differently. Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

护你周全 2024-11-07 05:55:26

使用strtok。这是一个参考:

http://www.metalshell.com/source_code/31/String_Tokenizer.html

/* strtok example by [email protected]
 *
 * This is an example on string tokenizing
 *
 * 02/19/2002
 *
 * http://www.metalshell.com
 *
 */

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

int main(int argc, char *argv[])
{
        int x = 1;
        char str[]="this:is:a:test:of:string:tokenizing";
        char *str1;

        /* print what we have so far */
        printf("String: %s\n", str);

        /* extract first string from string sequence */
        str1 = strtok(str, ":");

        /* print first string after tokenized */
        printf("%i: %s\n", x, str1);

        /* loop until finishied */
        while (1)
        {
                /* extract string from string sequence */
                str1 = strtok(NULL, ":");

                /* check if there is nothing else to extract */
                if (str1 == NULL)
                {
                        printf("Tokenizing complete\n");
                        exit(0);
                }
                /* print string after tokenized */
                printf("%i: %s\n", x, str1);
                x++;
        }
        return 0;
}

Uses strtok. Here's a reference:

http://www.metalshell.com/source_code/31/String_Tokenizer.html

/* strtok example by [email protected]
 *
 * This is an example on string tokenizing
 *
 * 02/19/2002
 *
 * http://www.metalshell.com
 *
 */

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

int main(int argc, char *argv[])
{
        int x = 1;
        char str[]="this:is:a:test:of:string:tokenizing";
        char *str1;

        /* print what we have so far */
        printf("String: %s\n", str);

        /* extract first string from string sequence */
        str1 = strtok(str, ":");

        /* print first string after tokenized */
        printf("%i: %s\n", x, str1);

        /* loop until finishied */
        while (1)
        {
                /* extract string from string sequence */
                str1 = strtok(NULL, ":");

                /* check if there is nothing else to extract */
                if (str1 == NULL)
                {
                        printf("Tokenizing complete\n");
                        exit(0);
                }
                /* print string after tokenized */
                printf("%i: %s\n", x, str1);
                x++;
        }
        return 0;
}
绿光 2024-11-07 05:55:26

我可能会做这样的事情:

sscanf(str_in, %[^,],%f,%c,%f,%c,%f",
    command,
    &packed_time,
    &fix_status,
    &packed_lat,
    &NS,
    &packed_long);

struct lat_long { 
    int degrees;
    double minutes;
};

lat_long lat, long;
lat.degrees = packed_lat / 100;
lat.minutes = packed_lat - lat.degrees;

long.degrees = packed_long / 100;
long.minutes = packed_long - long.degrees;

I'd probably do something like this:

sscanf(str_in, %[^,],%f,%c,%f,%c,%f",
    command,
    &packed_time,
    &fix_status,
    &packed_lat,
    &NS,
    &packed_long);

struct lat_long { 
    int degrees;
    double minutes;
};

lat_long lat, long;
lat.degrees = packed_lat / 100;
lat.minutes = packed_lat - lat.degrees;

long.degrees = packed_long / 100;
long.minutes = packed_long - long.degrees;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文