Expression to convert 2's complement to signed int

I’m using Ignition to collect data from a GE Multilin 850 and some of the register values are stored in 2’s complement instead of signed int. I created an expression tag to convert the OPC tag value to signed int. Below is what I came up with for an 8 bit register, is there an easier way to convert 2’s complement?

// Example: -12.34 stored as -1234 i.e. 64302

// register 1 byte 1:
// register 1 byte 2:

//conversion of 2’s complement. 16th bit will identify if the number is negative or not.

if ((getBit({[.]Main_Pwr1_PF_2sComp},7)), //change position 15 to 7 when using 8 bit register
//if first bit is true, then flip all bits, subtract 1 and add a negative.
toInt((binEnc(
if (getBit({[.]Main_Pwr1_PF_2sComp},0),0,1), //invert the bit value
if (getBit({[.]Main_Pwr1_PF_2sComp},1),0,1), //invert the bit value
if (getBit({[.]Main_Pwr1_PF_2sComp},2),0,1), //invert the bit value
if (getBit({[.]Main_Pwr1_PF_2sComp},3),0,1), //invert the bit value
if (getBit({[.]Main_Pwr1_PF_2sComp},4),0,1), //invert the bit value
if (getBit({[.]Main_Pwr1_PF_2sComp},5),0,1), //invert the bit value
if (getBit({[.]Main_Pwr1_PF_2sComp},6),0,1), //invert the bit value
if (getBit({[.]Main_Pwr1_PF_2sComp},7),0,1) //invert the bit value
)-1)*(-1)), //subtract one and add a negative to the number

    //if first bit is false, then convert to decimal number
	toInt(binEnc(
            getBit({[.]Main_Pwr1_PF_2sComp},0),
            getBit({[.]Main_Pwr1_PF_2sComp},1),
            getBit({[.]Main_Pwr1_PF_2sComp},2),
            getBit({[.]Main_Pwr1_PF_2sComp},3),
            getBit({[.]Main_Pwr1_PF_2sComp},4),
            getBit({[.]Main_Pwr1_PF_2sComp},5),
            getBit({[.]Main_Pwr1_PF_2sComp},6),
            getBit({[.]Main_Pwr1_PF_2sComp},7)
            )
            )
)

Can you try to better describe what you’re doing and provide an example of what the data looks like and why it’s wrong?

Two’s complement is the default representation for signed integer values so I’m really not sure what you’re trying to describe here.

1 Like

Modbus, I presume (been a while since I looked a a MultiLin) ? If you are reading with prefix “HRUS”, just use “HR” instead.

https://docs.inductiveautomation.com/display/DOC81/Modbus+Addressing#ModbusAddressing-ManuallySpecifyEachAddress

Well I guess I didn’t understand how signed integers are stored in the first place. Are all signed int stored as 2’s complement?

Certainly in Ignition, Java in general, and that’s the assumption in every one of the drivers as well.

Thanks for clarifying, makes much more sense now.