将数组(小端格式)转换为值(当值为负时) [英] converting array (in little endian format) to value (when value negative)

查看:97
本文介绍了将数组(小端格式)转换为值(当值为负时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我,当我得到的数据为负值时,如何将数据从小印度转换为整数.


下面的数据y数组在值为正数时工作正常,但在值为负数时出现错误.请有人可以帮助我吗?


X是我不在乎的字节.
数据y = {X,X,X,X,E8,3,0,0}



Rx_POS_CANopenData = y.DATA(7)和&H7F
Rx_POS_CANopenData = Rx_POS_CANopenData *&H100
Rx_POS_CANopenData = Rx_POS_CANopenData + y.DATA(6)
Rx_POS_CANopenData = Rx_POS_CANopenData *&H100
Rx_POS_CANopenData = Rx_POS_CANopenData + y.DATA(5)
Rx_POS_CANopenData = Rx_POS_CANopenData *&H100
Rx_POS_CANopenData = Rx_POS_CANopenData + y.DATA(4)

lblPostion.Text = Rx_POS_CANopenData



数据y将返回整数值1000.目前还不清楚,很可能是基于一些误解.我认为问题与字节序无关的另一个原因是,您的平台很可能是Windows,在所有指令集体系结构上,它都是低字节序的,它是在以下平台上实现的:
http://en.wikipedia.org/wiki/Little_endian#Endianness_and_operating_systems_on_architectures [ http://en.wikipedia.org/wiki/Two%27s_complement [
因此,很可能仅通过学习两个补码(和/或涉及的其他演示文稿)如何工作就可以解决您的问题.如果不是,请再问一个问题,但这一次是全面的.描述什么是输入,什么应该是输出,在两者上到底使用了什么数据表示形式,在两端都使用了什么平台,等等.

—SA


Can someone tell me how to get convert data from little Indian to integer when the data I get is a negative value.


Data y array below works fine when the value is positive but I get error when value negative. PLEASE CAN SOMEONE HELP ME?


X is the byte I don’t care about.
Data y = {X,X,X,X,E8,3,0,0}



Rx_POS_CANopenData = y.DATA(7) And &H7F
Rx_POS_CANopenData = Rx_POS_CANopenData * &H100
Rx_POS_CANopenData = Rx_POS_CANopenData + y.DATA(6)
Rx_POS_CANopenData = Rx_POS_CANopenData * &H100
Rx_POS_CANopenData = Rx_POS_CANopenData + y.DATA(5)
Rx_POS_CANopenData = Rx_POS_CANopenData * &H100
Rx_POS_CANopenData = Rx_POS_CANopenData + y.DATA(4)

lblPostion.Text = Rx_POS_CANopenData



Data y will return integer value 1000.

解决方案

Please see my comments to the question. It is very unclear and most likely based on some misconception. One more reason I think the problem has nothing to do with endianness is that most likely your platform is Windows, which is anyway little-endian on all instruction-set architectures it is implemented on:
http://en.wikipedia.org/wiki/Little_endian#Endianness_and_operating_systems_on_architectures[^].

As you ask about the problem with negatives, I would rather suspect that you don''t know how to work with the two''s complement integer presentation, simply because I knew many people who had similar problems because of that. Please see:
http://en.wikipedia.org/wiki/Two%27s_complement[^].

I have no idea what exactly do you want achieve and what is the data presentation on the "other side" (some CAN data, I guess), but on Windows it''s always little-endian two''s complement.

So, it''s very likely that you will be able to resolve your problem just by learning how two''s complement (and/or some other presentation involved) work. If not, ask another question, but this time in a comprehensive manner. Describe what''s on input and what should be on output, what exactly data presentation is used on both, which platform is used on both ends, etc.

—SA


这篇关于将数组(小端格式)转换为值(当值为负时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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