Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在嵌入式C中,如何将返回的十六进制值转换为;“无符号字符”;“可用”;整数;类型_C_Integer_Char_Hex - Fatal编程技术网

在嵌入式C中,如何将返回的十六进制值转换为;“无符号字符”;“可用”;整数;类型

在嵌入式C中,如何将返回的十六进制值转换为;“无符号字符”;“可用”;整数;类型,c,integer,char,hex,C,Integer,Char,Hex,我们正在为I2C接口编写代码, 其中,我们将16位十六进制数读取为两个8位十六进制MSB和LSB,并将这些值作为“无符号字符”返回 我们希望连接这些MSB和LSB“char”值,最后需要一个“Integer”值进行进一步处理 例如:以下两个方法分别返回一个“Unsigned Char”值 (一) unsigned char i2c\u readAck(void) { TWCR=(1您只需要以下内容: unsigned char msb = ...; // read MSB unsigned ch

我们正在为I2C接口编写代码, 其中,我们将16位十六进制数读取为两个8位十六进制MSB和LSB,并将这些值作为“无符号字符”返回

我们希望连接这些MSB和LSB“char”值,最后需要一个“Integer”值进行进一步处理

例如:以下两个方法分别返回一个“Unsigned Char”

(一)

unsigned char i2c\u readAck(void)
{

TWCR=(1您只需要以下内容:

unsigned char msb = ...; // read MSB
unsigned char lsb = ...; // read LSB
int val = (msb << 8) | lsb; // combine MSB and LSB to make an int
无符号字符msb=…;//读取msb
无符号字符lsb=…;//读取lsb

int val=(msb是的,这很棘手,很难可视化。请记住,您(可能)得到的msb和LSB是您需要的实际二进制表示形式;您不需要转换这些表示形式。i2c就是这样工作的。因此:

  int number = 0, msb, lsb;

  msb = 0xff & read_one_unsigned_i2c_byte();
  lsb = 0xff & read_one_unsigned_i2c_byte();

  number = msb << 8;      // gotta' shift msb to upper 8 bits of final result
  number = number | lsb;  // and then IOR in the lower 8 bits
int number=0,msb,lsb;
msb=0xff&读取一个无符号i2c字节();
lsb=0xff&读取一个无符号i2c字节();

number=msb paul,我们对这个嵌入式c非常陌生,所以再问一次这个问题,请不要介意,因为msb和lsb值的数据类型是“char”,我们可以直接对它执行位操作吗?然后我们可以直接将它存储在“int”上吗没有类型转换。这不是一个类型转换问题吗?谢谢你paul,马上回答它…!…请回答我上面的问题,@Balaji KR:char只是一个小int,在上面的表达式中它将被隐式提升为int-我建议你试试上面的代码,看看它是否适合你。@paul…非常感谢你。@I waIt’我以前在研究java……我的java理论太多了,这让我用C问了这么愚蠢的问题……哈哈……非常感谢你的回答……你的答案很好!你应该使用
无符号字符
而不是
字符
来保持
msb
lsb
,因为
字符
>可能是有符号的。移位和按位or将给出负值的完全错误的结果。非常感谢pete,实际上我开始研究java,我害怕在每一步编写代码时向前推进,因为我在其中可视化了类型转换错误..!正如我所说,这只是可视化,我后退一步编写代码..lol.Yes,lol,但不用担心。当你尝试上面给出的两种解决方案中的一种时,你会发现它像冠军一样有效。
unsigned char msb = ...; // read MSB
unsigned char lsb = ...; // read LSB
int val = (msb << 8) | lsb; // combine MSB and LSB to make an int
  int number = 0, msb, lsb;

  msb = 0xff & read_one_unsigned_i2c_byte();
  lsb = 0xff & read_one_unsigned_i2c_byte();

  number = msb << 8;      // gotta' shift msb to upper 8 bits of final result
  number = number | lsb;  // and then IOR in the lower 8 bits