Author Topic: INT32 to signed integer when byte order is reversed  (Read 5582 times)

pixelfantastic

  • Newbie
  • Posts: 3
    • View Profile
INT32 to signed integer when byte order is reversed
« on: May 16, 2011, 11:18:41 AM »
Hi guys,
can anyone please help?

I have a 4 bytes INT32 coming in via rs232, and the byte order is lowbyte first
 (LSB -- low, high, low,high -- MSB)

how can i  convert this to signed integer ?
result should be from -2147483648  to +2147483647

Thanks for your assistance
Barrie

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3171
    • View Profile
    • Internet Programmable PLCs
Re:INT32 to signed integer when byte order is reversed
« Reply #1 on: May 16, 2011, 11:38:29 AM »
Since the data come in through RS232 port, are you able to read them into DM[] area using INCOMM command?

If so then each byte would have been stored DM and you can then rearrange the DM to convert into 32-bit number.

E.g. if the data are stored in DM[1] to DM[4], you can covert into 32-bit integer as follow:

DM[5] = DM[4]*256+DM[3]
SETHIGH16 A, DM[5]
A = A + DM[2]*256+DM[1]

A will contain the signed 32-bit numbers that is converted from DM[1] to DM[4], with DM[4] being the most significant byte.
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

pixelfantastic

  • Newbie
  • Posts: 3
    • View Profile
Re:INT32 to signed integer when byte order is reversed
« Reply #2 on: May 16, 2011, 12:04:58 PM »
Thanks for that!

i worked perfectly

Barrie