cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar

Is there a function to convert a character to its ASCII equivalent?

Is there a function to conver a character to an ascii number?

 

In the end I need to translate a character based column in data into its numerical equivalent.  They always happen to be 1 or 2 digits.  So effectively 

 

"A" becomes 1

"Z" becomes 26

"AA" becomes 27

"AB" becomes 28

 

etc.

 

So say DIE_COLUMN = "CA"  I want to return 79

 

I've come up with the following thoroughly ugly formula... but if there was a char to ascii function I could do a lot better.   There HAS to be a cleaner way.

If( Length( :DIE_COLUMN ) == 2,
	Contains( "ABCDEFGHIJKLMNOPQRSTUVWXYZ", Left( :DIE_COLUMN, 1 ) ) * 26,
	0
) + Contains( "ABCDEFGHIJKLMNOPQRSTUVWXYZ", Right( :DIE_COLUMN, 1 ) )
3 REPLIES 3
txnelson
Super User

Re: Is there a function to convert a character to its ASCII equivalent?

For the translation you are doing, your function works.  If an ASCII to Decimal function was used, the character "A" would return 65 

txnelson_0-1651162823000.png

 

Jim
Craige_Hales
Super User

Re: Is there a function to convert a character to its ASCII equivalent?

Translating 2 character codes to a number is a good job for an associative array. You could create the aa with nested for loops. You might want to force lower case on the keys .
Craige
pmroz
Super User

Re: Is there a function to convert a character to its ASCII equivalent?

This will do it:

die_column = "CA";
If( Length( DIE_COLUMN ) == 2,
	(num(hex(Left( DIE_COLUMN, 1 ) )) - 40) * 26,
	0
) + num(hex(Right( DIE_COLUMN, 1 ) )) - 40

You would need to uppercase die_column.  This works because A is ASCII 41.