簡體   English   中英

掃描十六進制字符串的值

[英]Scan values of hex string

我有一個像這樣的十六進制字符串00133587a1bddb8dae00a3a01a010100 ,實際上是7個十六進制字符串串聯在一起,當擴展出來時看起來像這樣00 133587a1 bddb8dae 00a3a01a 01 01 00 我正在嘗試將這些值的前5個掃描到此結構中

typedef struct __param_value{
    uint8_t sytem_id;
    uint8_t comp_id;
    uint16_t seq;
    uint8_t frame;
    uint16_t command;
    uint8_t current;
    uint8_t autocontinue;
    float param1;
    float param2;
    float param3;
    float param4;
    float x;//param7
    float y;//param8
    float z;//param9
    uint8_t fwt;

}param_value

最后2個進入這些變量

    int txtseq;
    int cont=1;

通過使用sscanf,像這樣

sscanf(in_str,"%2x%8x%8x%8x%2x%2x%2x",&(points[wp].seq),&(points[wp].x),&(points[wp].y),&(points[wp].z),&(points[wp].fwt),&txtseq,&cont);

但我找不到正確的語法。 有可能這樣做嗎?

您必須傳遞unsigned int地址來掃描%x格式說明符的數據。 然后,您可以轉換為正確的數據類型。

#include <stdio.h>

typedef unsigned char uint8_t;
typedef unsigned short uint16_t;

typedef struct param_value{
    uint8_t sytem_id;
    uint8_t comp_id;
    uint16_t seq;
    uint8_t frame;
    uint16_t command;
    uint8_t current;
    uint8_t autocontinue;
    float param1;
    float param2;
    float param3;
    float param4;
    float x;//param7
    float y;//param8
    float z;//param9
    uint8_t fwt;
}param_value;

int main(void) {
    char hexstr [] = "00133587a1bddb8dae00a3a01a010100";
    unsigned v1, v2, v3, v4, v5, v6, v7;
    param_value rec;
    int txtseq;
    int cont;

    if (7 != sscanf(hexstr, "%2x%8x%8x%8x%2x%2x%2x", &v1, &v2, &v3, &v4, &v5, &v6, &v7))
    {
        printf ("Bad scan\n");
        return 1;
    }

    rec.seq = (uint16_t)v1;
    rec.x   = (float)v2;
    rec.y   = (float)v3;
    rec.z   = (float)v4;
    rec.fwt = (uint8_t)v5;
    txtseq  = (int)v6;
    cont    = (int)v7;

    printf("%u %f %f %f %u %d %d\n", rec.seq, rec.x, rec.y, rec.z,
                                     rec.fwt, txtseq, cont);
    return 0;
}

程序輸出:

0 322275232.000000 3185282560.000000 10723354.000000 1 1 0

但是:您尚未提到ybddb8dae是否打算為負數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM