簡體   English   中英

如何獲取十進制數 position

[英]How to get number at decimal position

我正在研究用於微控制器的 C 代碼,以將 GPS 數據格式化為一個 8 字節消息。

我需要對其進行編碼並快速解碼。 我想這樣做:Int32 是 -2147483648 到 2147483647。

我們可以編碼緯度:-89599999 到 -89599999 position 89° 59.9999' S 到 90° 59.9999' N
對於經度:-179599999 到 179599999 position 179° 59.9999' W 到 179° 59.9999' E

緯度 4 個字節,經度 4 個字節。 總共 8 個字節的 CAN 消息。

沒有乘除法怎么辦? 有沒有辦法以想要的十進制 position 移動或獲取數字。

我想以這種方式編碼:

unsigned int32 charToInt32(char val)
{
   unsigned int32 valOut;
   valOut = val - '0';
   return valOut;
}

int32 GPS_codeLongitude(char *tab, char dir)
{
   int32 deg = 0;  

   deg  = charToInt32(tab[0]) * 1000000000; 
   deg += charToInt32(tab[1]) * 100000000;
   deg += charToInt32(tab[2]) * 10000000;
   deg += charToInt32(tab[3]) * 1000000;
   deg += charToInt32(tab[4]) * 100000;
   deg += charToInt32(tab[6]) * 10000;
   deg += charToInt32(tab[7]) * 1000;
   deg += charToInt32(tab[8]) * 100;
   deg += charToInt32(tab[9]) * 10; 
   deg += charToInt32(tab[10]); 

   if(dir == 'S') deg = -deg;
   return deg;
}

int32 GPS_codeLatitude(char *tab, char dir)
{
   int32 deg = 0;  

   deg  = charToInt32(tab[0]) * 100000000; 
   deg += charToInt32(tab[1]) * 10000000;
   deg += charToInt32(tab[2]) * 1000000;
   deg += charToInt32(tab[3]) * 100000;
   deg += charToInt32(tab[5]) * 10000;
   deg += charToInt32(tab[6]) * 1000;
   deg += charToInt32(tab[7]) * 100;
   deg += charToInt32(tab[8]) * 10;
   deg += charToInt32(tab[9]) ; 

   if(dir == 'W') deg = -deg;
   return deg;
}

這是一種方法:

static void int2str(char *buf, unsigned int x)
{
    const unsigned int divs[] = { 1000000000, 100000000, 10000000, 1000000,
     100000, 10000, 1000, 100, 10 };
    for (int i = 0; i < sizeof divs / sizeof *divs; ++i)
    {
        unsigned char digit = '0';
        while (x >= divs[i])
        {
            ++digit;
            x -= divs[i];
        }
        *buf++ = digit;
    }
    *buf++ = '0' + x;
    *buf = '\0';
}

這將向左填充零,但這對於計算機對計算機的對話通常是可以的。 此外,它不使用任何乘法或除法,但它非常迭代,所以不是很快......

使用 atoi 和按位運算的示例:

#define CanMessageLen 8
#define NIBBLELEN 4
typedef char CanMessage[CanMessageLen];


typedef enum
{
    NORTH,
    SOUTH
} LatitudeDir;

typedef enum
{
    EAST,
    WEST
} LongitudeDir;

CanMessage* CoordinatesToMsg(char* latitude, LatitudeDir latitudedir, char* longitude , LongitudeDir longitudedir)
{
    CanMessage canMessage = { 0 };
    __int32 latitudeInt=0;
    __int32 longitudeInt = 0;

    latitudeInt = atoi(latitude);
    longitudeInt = atoi(longitude);

    if (latitudedir == SOUTH)
    {
        latitudeInt = -latitudeInt;
    }
    if (longitudedir == WEST)
    {
        longitudeInt = -longitudeInt;
    }

    for (size_t i = 0; i < 4; i++)
    {
        canMessage[i] = (char)(latitudeInt >> (8 * i) & 0xff);
        canMessage[i+4] = (char)(longitudeInt >> (8 * i) & 0xff);
    }
    return canMessage;
}

示例 -80° 55.9999' 150° 55.9999

char latitude[10] = { '0','8','0','5','5','9','9','9','9','\0'}; 
char longitude[10] = { '1','5','0','5','5','9','9','9','9','\0'}; 

CoordinatesToMsg(latitude, SOUTH,longitude, EAST);

Output [0x81,0xC0,0x32, 0xFB, 0xFF, 0x5C, 0xF9, 0x08] //小端

暫無
暫無

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

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