Convert ASCII Characters to ASCII Codes in LabVIEW

Updated Sep 26, 2023

Reported In

Software

  • LabVIEW

Issue Details

I am creating an instrument control application in which I need to build command strings that contain unprintable ASCII characters.
  • What is the easiest way to produce an arbitrary ASCII character based on the known ASCII code for the character?
  • How can I obtain the ASCII code for an arbitrary character that I'm reading into a string buffer?

Solution

  •  Convert an ASCII character into its corresponding ASCII code 
Wire the character into the left-hand input of Type Cast VI. In this case, you must provide an input to the Type terminal of the Type Cast VI, because you do not want the default string output. Wire a constant U8 into the Type terminal. The output will now be the U8 ASCII code that corresponds to the input character.

  • Convert an integer with an ASCII code into its corresponding ASCII character
Wire the ASCII code into the left-hand input of the Type Cast VI, and make sure that its format is U8 (a single ASCII character or code occupies only a single byte's worth of information). The output of the Type Cast VI will be the desired ASCII character. You can incorporate this character into an instrument command string using the String Concatenate VI.

 
  • ​Convert an ASCII character into its corresponding hexadecimal ASCII code
  • Entering non-displayable characters in a string control
By right clicking on a string control it can be changed to Normal Display,'\' Codes Display, Password Display, or Hex Display. Non-displayable characters can be entered in '\' Codes Display by using their corresponding escape codes or a backslash and the hexadecimal equivalent. For example, a tab would be \t or \09 . In hex display each character is represented by its corresponding hexadecimal equivalent. 

Additional Information

It is important to note that the String/Number Conversion sub-palette provides different functionality from the typecast functions described in this article. This palette is made to convert a number to a string (eg. 64 » "64") representing the same number rather than treating each number as the ASCII code representing the number (eg. 64 » "@").