二进制转换为int [英] Converting binary to int

查看:322
本文介绍了二进制转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我事先道歉,如果这是一个初学者的问题;我是新的C语言编程。

I apologize beforehand if this is a beginner's question; I'm new to C programming.

我工作的一个嵌入式设计项目,需要我的ATmega128的来自传感器的4个字节的数据读取。该数据包含我需要处理两个14位的测量。还有一个部分我不能完全肯定,因为我没有做了很多的C编程呢。我的算法如下:

I am working on an embedded design project that requires my ATmega128 reads in a 4-byte data from a sensor. The data contains two 14-bit measurements that I need to process. There is one part I'm not completely sure about because I haven't done a lot of C programming yet. My algorithm is as follows:

1)接收4字节数据从传感器(MS 2位和LS 2位将被扔)
2)除每个字节到4 unsigned char型变量
3)屏蔽掉MS从MS字节2位,同样来自LS字节LS 2位
4)MS 2个字节=第一次测量,LS 2个字节=第二次测量

1) Receive 4 bytes of data from sensor (MS 2 bits and LS 2 bits are to be tossed) 2) Save each byte into 4 unsigned char variables 3) Mask out MS 2 bits from MS byte, same for LS 2 bits from LS byte 4) MS 2 bytes = first measurement, LS 2 bytes = second measurement

这部分,我目前可以做的。什么我不知道的是如何转换ç二进制为十进制。我需要把我的两个14位的测量成unsigned int类型,使其更容易进行计算。是否有自动执行此操作的指令?或将编译器会自动把它变成一个int,如果我在使用它的整数操作?或者,也许没有这些,我必须写我自己的函数将其转换?

This part I can do so far. What I'm not sure about is how C converts binary to decimal. I need to turn my two 14-bit measurements into unsigned int to make it easier to perform calculations. Is there an instruction that automatically does this? Or will the compiler automatically turn it into an int if I use integer operations on it? Or maybe none of these, and I have to write my own function to convert it?

推荐答案

所有的C数据被再次presented二进制,所以unsigned char型的值是真的只是整数。他们是psented二进制重新$ P $,但你可以用它们作为数字。所以,+, - ,*,等方面的工作,就像你所期望的(只是注意/因为整数除法和浮点分工是不同的)

All C data is represented as binary, so unsigned char values are really just integers. They are represented as binary, but you can just use them as numbers. So +, -, *, etc. work just as you would expect (just watch out for / because integer division and float division are different).

这篇关于二进制转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆