我有 2 个字符,HIGH 和 LOW,我想将它们转换为int
对应于 HIGH + LOW 的 2 个左位。
你的规格不清楚。鉴于:
unsigned char HIGH = 152;
unsigned char LOW = 12;
这可能意味着:
int result = HIGH + (LOW >> 6); // Assuming CHAR_BIT == 8
或者它可能意味着:
int result = HIGH + (LOW & 0xC0);
或者它可能意味着:
int result = (HIGH << 2) | (LOW >> 6);
或者它可能有其他含义。对于问题中显示的值,前两个表达式产生相同的答案。
To get a more meaningful answer, you'll have to spell out the requirements much more carefully. Your actual code bears almost no discernible relationship to the requirement you specify. You have two char *
variables; you initialize them to what are almost certainly invalid addresses; you don't initialize result
. Your computations are, at best, odd.