Skip to content

void CommProtocolReceiveUartData(unsigned char *buf, int len) #7

@junlon2006

Description

@junlon2006

切记,一切非字符串的均用“unsigned char”
https://stackoverflow.com/questions/75191/what-is-an-unsigned-char

char负数在向上转换为unsigned时(u16_t,高位扩展为1,即-1将扩展为FFFF而不是FF)
而unsigned char高位总是不会扩展

见下面代码

#include <stdio.h>

static void __char_2_u16(char c) {
  unsigned short u16 = (unsigned short) c;
  printf("%0x\n", u16);
}

int main() {
  __char_2_u16('a');
  __char_2_u16('0');
  __char_2_u16(-1);

  return 0;
}

61
30
ffff

#include <stdio.h>

static void __char_2_u16(unsigned char c) {
  unsigned short u16 = (unsigned short) c;
  printf("%0x\n", u16);
}

int main() {
  __char_2_u16('a');
  __char_2_u16('0');
  __char_2_u16(-1);

  return 0;
}

61
30
ff

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions