Blog

Text - How to convert a character to its ASCII numeric value

There's no built-in function in Power Apps to convert a character to its ASCII value. This post describes how to carry out this task.

With Power Apps, the Char function takes a numeric ASCII code and returns the character representation.

http://powerappsguide.com/blog/post/how-to-use-char-function

Unfortunately, there's no built-in function to carry out the inverse of Char - that is, a function that returns the numeric ASCII code for an input character. It's useful to note that Excel VBA includes a function called ASC that carries out this task, and app builders familiar with Excel often search for a Power Apps equivalent.

To carry out this task, we need to write custom formula, and we can use the structure that's shown below.

With({inputString:"a"},
LookUp(
ForAll(Sequence(255),
{Num:Value, Character:Char(Value)}
),
Character=inputString
).Num
)
We set inputString to the character to convert. As the screenshot beneath illustrates, the formula returns the correct ASCII code for the input character "a", which is 97.

How does this formula work?

The formula applies the ForAll function to generate a table with 2 columns - the numeric ASCII code and the corresponding character. The screenshot below highlights the return value of this section of formula.


To retrieve the ASCII code that corresponds to a given input character, the outer call to LookUp retrieves the numeric value for the target character value.

Conclusion

Power Apps provides no built-in function to convert an input character to an ASCII code (ie, an Excel VBA ASC equivalent). This post described a formula to carry out this task.
  •   Categories: 
  • text