我sprintf_s
以这种格式将函数转换为 C99。只是想确保,结果是一样的:
sprintf_s(char_star,"%08X",some_int);
char_star 的大小为 8。它转换some_int
为 8 个字节的char_star
.
那么我在 C99 中所做的正确吗?我使用了以下函数之一,虽然这些 C99 函数和 C11 Annex K 的输出sprintf_s
不一样
char str[9];
snprintf (str, 8, "%08x", i );
// sprintf(str, "%08x", i);
QByteArray built(str, 8);
qDebug() << built.toHex();
在 MFC 中,它给了我这个包和 sprintf
30 30 30 31 30 30 44 43
在 C99 (linux glibc)
sprintf(str, "%08x", i)
中给出
30 30 30 31 30 30 64 63
你知道问题出在哪里吗?
这是整个功能:
bool BuildAnglePacket(char* WR_PacketAZAngle, float AZAngle)
{
WR_PacketAZAngle[0] = 0x04;
WR_PacketAZAngle[1] = 0x30;
WR_PacketAZAngle[2] = 0x31;
WR_PacketAZAngle[3] = 0x02;
WR_PacketAZAngle[4] = 0x79;
WR_PacketAZAngle[5] = 0x4E;
WR_PacketAZAngle[6] = 0x48;
int XOR;
char HAnlge[9];
int iAzimuthAngle;
if (AZAngle >= -22.5 && AZAngle <= 22.5 )
{
iAzimuthAngle = AZAngle*10;
if( AZAngle < 0)
{
iAzimuthAngle= abs(iAzimuthAngle);
iAzimuthAngle=((~iAzimuthAngle)&0xFFFF) + 1 ;
}
iAzimuthAngle=65536 + iAzimuthAngle;
sprintf(HAnlge,"%08x", iAzimuthAngle);
WR_PacketAZAngle[7] = HAnlge[0];
WR_PacketAZAngle[8] = HAnlge[1];
WR_PacketAZAngle[9] = HAnlge[2];
WR_PacketAZAngle[10] = HAnlge[3];
WR_PacketAZAngle[11] = HAnlge[4];
WR_PacketAZAngle[12] = HAnlge[5];
WR_PacketAZAngle[13] = HAnlge[6];
WR_PacketAZAngle[14] = HAnlge[7];
WR_PacketAZAngle[15] = 0x03;
for(int i=4;i<16;i++)
XOR ^= WR_PacketAZAngle[i];
WR_PacketAZAngle[16] = XOR;
WR_PacketAZAngle[17] ='\x0';
}
return true;
}