2转换成字节的整数。 [英] Convert 2 bytes into an integer
本文介绍了2转换成字节的整数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到为2个字节(至少显著字节在前)的端口号,我想将它转换为整数,这样我可以使用它。我做了这样的:
字符BUF [2]; //当接收到的字节是焦炭端口[2];端口[0] = buf中[1];端口[1] = buf中[0];INT数= 0;数=(*((INT *)端口));
不过,有一些错误的,因为我没有得到正确的端口号。有任何想法吗?
解决方案
我收到的2字节的端口号(至少显著字节在前)
块引用>您就可以做到这一点:
INT数= BUF [0] | BUF [1];&下; 8;
I receive a port number as 2 bytes (least significant byte first) and I want to convert it into an integer so that I can work with it. I've made this:
char buf[2]; //Where the received bytes are char port[2]; port[0]=buf[1]; port[1]=buf[0]; int number=0; number = (*((int *)port));
However, there's something wrong because I don't get the correct port number. Any ideas?
解决方案I receive a port number as 2 bytes (least significant byte first)
You can then do this:
int number = buf[0] | buf[1] << 8;
这篇关于2转换成字节的整数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文