如何在 C 中将 2 个二进制文件转换为短格式
How to convert 2 binaries to a short in C
签名:
short convetToSignedShort(uint8_t first, uint8_t second)
{
signed short j;
j = (signed char)first + (signed char)second;
printf("signed short: %d\n", j);
return j;
}
未签名:
short convetToUnsignedShort(uint8_t first, uint8_t second)
{
unsigned short j;
j = (unsigned short)first + (unsigned short)second;
printf("unsigned short: %d\n", j);
return j;
}
我知道我没有使用正确的方法。
例如,我需要 0x01 0x01
成为 257
,我该怎么做?
对无符号做这样的事情:
uint16_t convertToUnsignedShort(uint8_t high, uint8_t low)
{
uint16_t j;
j = ((uint16_t)high << 8) + (uint16_t)low;
printf("unsigned short: %d\n", j);
return j;
}
对于已签名的,意义不大。你期望低字节的符号位会发生什么?
签名:
short convetToSignedShort(uint8_t first, uint8_t second)
{
signed short j;
j = (signed char)first + (signed char)second;
printf("signed short: %d\n", j);
return j;
}
未签名:
short convetToUnsignedShort(uint8_t first, uint8_t second)
{
unsigned short j;
j = (unsigned short)first + (unsigned short)second;
printf("unsigned short: %d\n", j);
return j;
}
我知道我没有使用正确的方法。
例如,我需要 0x01 0x01
成为 257
,我该怎么做?
对无符号做这样的事情:
uint16_t convertToUnsignedShort(uint8_t high, uint8_t low)
{
uint16_t j;
j = ((uint16_t)high << 8) + (uint16_t)low;
printf("unsigned short: %d\n", j);
return j;
}
对于已签名的,意义不大。你期望低字节的符号位会发生什么?