How to Convert Hexadecimal to Decimal or ASCII in LabVIEW?

Updated May 16, 2019

Reported In

Software

  • LabVIEW Full
  • LabVIEW Base
  • LabVIEW

Issue Details

  • In LabVIEW, how do I convert a hexadecimal value to a decimal value? Currently, I am trying to use the Scan Value Function but am unsure what I need to connect to it.
  • I am communicating with an instrument that sends back information in the form of hexadecimal values. For instance, instead of sending the letter L, it sends back its corresponding hexadecimal representation 4C. How can I programmatically convert this hexadecimal representation into its corresponding ASCII representation in LabVIEW? What if I need to send information back to the instrument in hexadecimal?

Solution

Converting Hexadecimal to Decimal 

  1. To convert the hexadecimal string to its decimal representation, use the Scan Value Function. This function is located in the Functions Palette under Programming»String»String/Number Conversion
  2. Right-click on the string input terminal of this VI and select Create»Control. This will generate a string contstant.
  3. Right-click on the format string input terminal of this VI and select Create»Constant. To specify Hexadecimal format, input %x in this constant. 
  4. Right-click on the default (0dbl) input terminal of this VI and select Create»Constant. Change the representation of this constant to Integer. To do this right-click on the constant and select Representation. Then select the corresponding integer that works best for the range of values you need. The range of values for each data type is outlined in the Numeric Data Types Table
  5. Right-click on the value output terminal of this VI and select Create»Indicator 
  6. An example of the end result using these steps is shown below:

Converting Hexadecimal to ASCII

  1. To convert a hexadecimal string into an ASCII string, you must first convert the hexadecimal string to its decimal representation using the Scan Value function. First complete steps 1-5 in the Converting Hexadecimal to Decimal subsection above.
  2. You can then convert the decimal representation to its ASCII string equivalent using the Type Cast Function, found in the Functions Palette under Mathematics»Numeric»Data Manipulation.
  3. Connect the wire connected to the Decimal Indicator to the x input terminal.
  4. Right-Click on the *(type*) &x output terminal of the Type Cast Function and select Create»Indicator. 
  5. An example of the end result using these steps is shown below:

Converting to ASCII to Hexadecimal

  1. To convert the ASCII string to its hexadecimal representation, use the Number to Hexadecimal String Function. This function is located in the Functions Palette under Programming»String»String/Number Conversion.
  2. Right-click on the hex integer string output interminal of this VI and select Create»Indicator. 
  3. Place the Type Cast Function on the Block Diagram. This VI found in the Functions Palette under Mathematics»Numeric»Data Manipulation.
  4. Wire a 0 value Numeric Constant into the type terminal of the Type Cast Function.
  5. Click on the *(type*) &x output terminal of the Type Cast Function and wire it to the number input terminal of the Number to Hexadecimal String Function.
  6. Wire a String Constant into the x terminal of the Type Cast Function.
  7. An example of the end result using these steps is shown below:

    Additional Information

    The hexadecimal string input for the above VIs are in the form FF not 0xFF. The 0x is the convention for indicating that a number is hexadecimal based rather then decimal based. A standard hexademical string does not carry this indicator, and it must be removed before the value can be translated. This behavior is identical to the Hexadecimal String to Number VI and is expected.  If you have strings which have the 0xFF format you can use a Search/Split String VI with a search character of x before the string is passed to the Scan Value VI.

    WAS THIS ARTICLE HELPFUL?

    Not Helpful