Author Topic: convert hex number to decimal number  (Read 14301 times)

kenobe

  • Jr. Member
  • Posts: 66
  • I'm a llama!
    • View Profile
convert hex number to decimal number
« on: January 14, 2010, 10:19:46 PM »
Hi
I can not find a function in Tbasic to convert hex number to dec number.
Can some one pls help.
Thanks

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:convert hex number to decimal number
« Reply #1 on: January 15, 2010, 05:31:26 AM »
HEX and DEC only make sense to human. There is no underlying difference to the PLC between a DEC and HEX number since they are the same number. So you can't convert from dec to hex or dec to hex.

However, you can convert a "hex string" to a number using the "HEXVAL()" function. Likewise you can convert a number into hex string using the HEX$() function.
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

kenobe

  • Jr. Member
  • Posts: 66
  • I'm a llama!
    • View Profile
Re:convert hex number to decimal number
« Reply #2 on: January 15, 2010, 06:15:35 PM »
HI
Thanks for your advise.
The reason I need to convert a Hex number to Dec number is that
I need to send a telephone number (dec) to a modem to send SMS message( of course I ve to convert the dec number to string), the input device ( touch screen) responsible for capturing the user input feeds the T100MD888 with hex number and stored in DM[1] and DM[2]. I therefore need to extract the numbers from DM[1] and DM[2], convert them to dec numbers and later to strings and join them together.
I hope this explains my problem better.
Thanks

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:convert hex number to decimal number
« Reply #3 on: January 17, 2010, 07:53:43 AM »
How does your touch screen store data into the DM[1] and DM[2]? I imagine it is via Modbus? If so, then the number that you enter as Hex in your touch screen is sent to the PLC DM[1] and DM[2] as an integer number. A number is always a binary number inside the CPU.  Whether we look at it as decimal or hexadecimal is only for our own convenience since a number that is represented by 0 and 1 only is not easily readable by human.

You can understand what I mean by going online monitoring and then view the number as decimal or as hex by pressing the H or D key while in the "View variable" screen. The screen will show the number in either format. But that does not change the number itself.

When you need to format the number as a hexadecimal string, you can simply use the function HEX$(DM[1],4) to convert it into 4 digit hexadecimal number.
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

kenobe

  • Jr. Member
  • Posts: 66
  • I'm a llama!
    • View Profile
Re:convert hex number to decimal number
« Reply #4 on: January 17, 2010, 09:46:02 PM »
HI
Your suggestion works .
Thanks