Home AMX User Forum NetLinx Studio

Negative 32 bit conversion

Hi there,

To display a negative voltage from a device (that should be -15.20), I get from the device FA10

I have seen that hex FFFFFFFFFFFFFA10 is actually the presentation of -1520 but how to display -1520 ?

then convert to float is simple...

Comments

  • ericmedleyericmedley Posts: 4,177
    When you say 'display' do you mean on a touch panel or are you referring to using the value in a calculation? There are obviously ways but a little more info would help.
  • criss87criss87 Posts: 32
    Thanks for the reply, I just need to store the negative value into a variable and of then sow on the touch panel.
    The problem is just the conversion of the number from FFFFFFFFFFFFFA10 to -1520.

    Criss
  • a_riot42a_riot42 Posts: 1,624
    criss87 wrote: »
    Thanks for the reply, I just need to store the negative value into a variable and of then sow on the touch panel.
    The problem is just the conversion of the number from FFFFFFFFFFFFFA10 to -1520.

    Criss

    itoa or ftoa is what you need to convert an integer or float to a string to show on a touch panel. Any text on a touch panel has to be of char type.
    Paul
  • PhreaKPhreaK Posts: 966
    In what format is 0xFFFFFFFFFFFFFA10 equivalent to decimal -1520?

    edit:
    My bad, two's compliment *facepalm*.
  • PhreaKPhreaK Posts: 966
    So with the above in mind your conversion is going to be a two step process. First you need to pass that character array into a single value with all the bits in the right place. If its always going to be contained in your lower 4 bytes you can use the function in the NCL math library. This will make it a bit easier as you can pass that into a single long for the next step.

    Lets say you pass this into the variable x:
    x = math_raw_be_to_long(<you're four character array>)
    

    This assumes that what came from the device was in fact "$FA, $10" - if its an ascii representation of it you'll have to convert that first as well.

    Once you have that you need to do the conversion from two's compliment to straight up, normal, human readable decimal. To do that you first need to subtract 1 then flip all the bits:
    decimalVal = (x - 1) ^ $FFFFFFFF
    
    .

    Alternatively, if it always only going to be 2 bytes with the value the following will work:
    value = ((yourData[1] << 8 + yourData[2]) - 1) ^ $FFFF
    
  • criss87criss87 Posts: 32
    Thanks PhreaK! this will help me a lot!

    Criss
Sign In or Register to comment.