将2 char转换为1 int [英] Convert 2 char into 1 int
本文介绍了将2 char转换为1 int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个字符:HIGH和LOW,我想将它们转换为一个int对应于HIGH +从左边的2个左位。
I have 2 chars: HIGH and LOW and I'd like to convert them to an int corresponding to HIGH + the 2 left bits from LOW.
有人喜欢:
unsigned char high;
unsigned char low;
high = 128; // 10000000
low= 128; // 10000000
int result; (should be high 10000000 + 2 left bites of low 10 = 1000000010)
// To do
return result;
编辑为了更清楚。
我选择的解决方案是:
return high*4 + (low >> (CHAR_BIT - 2));
推荐答案
您声明 HIGH
和
LOW
作为 char *
,但不要将它们用作指针。下面的代码工作正常(BTW,避免大写标识符,当你不使用常量):
You declare HIGH
and LOW
as char*
, but you don't use them as pointer. The following code works fine (BTW, avoid upper case identifiers when you don't use constants):
char high = 125;
char low = 12;
这是我的理解你的问题(它可以更容易理解):
This is how I understand your question (it could be more understandable):
#include <limits.h>
int result = high + (low >> (CHAR_BIT - 2));
这篇关于将2 char转换为1 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文