Click here to Skip to main content
15,121,564 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Comments
Sergey Alexandrovich Kryukov 16-Jul-12 17:58pm
   
"Little endian" and "integer" are not alternatives. You surely mean something else. Two's complement vs. some other presentation? What? What's the structure of input data?
--SA
Member 8525993 17-Jul-12 9:00am
   
if i got 32 bit data that is little endian format

{a,b,c,d}

to find decimal value for it i would do
Rx_POS_CANopenData = d And &H7F
Rx_POS_CANopenData = Rx_POS_CANopenData * &H100
Rx_POS_CANopenData = Rx_POS_CANopenData +c
Rx_POS_CANopenData = Rx_POS_CANopenData * &H100
Rx_POS_CANopenData = Rx_POS_CANopenData + b
Rx_POS_CANopenData = Rx_POS_CANopenData * &H100
Rx_POS_CANopenData = Rx_POS_CANopenData + a



the decimal value that i would get for Rx_POS_CANopenData will be 1000. what happen if the decimal value is negative ?
Sergey Alexandrovich Kryukov 16-Jul-12 18:04pm
   
And tag the platform as well. Is it Windows? Tag it.
--SA

1 solution

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
   
Comments
Member 8525993 17-Jul-12 10:33am
   
thanks you . i will look into this.
Sergey Alexandrovich Kryukov 17-Jul-12 11:59am
   
You are welcome. And when you find that's the point, please accept this answer formally (green button).
If not, you are welcome to ask follow-up questions, but in this case please provide comprehensive information.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900