Пример с использованием 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);
Вывод [0x81,0xC0,0x32, 0xFB, 0xFF, 0x5C, 0xF9, 0x08] // Little Endian