Re: character to ASCII conversion
Posted in 1995
> Date: Mon, 13 Mar 1995 07:58:44 -0800 > From: johnl@informix.com (Jonathan Leffler) > To: informix-list@rmy.emory.edu, pamr@fritz.nsaho.ns.ca > Subject: Re: character to ASCII conversion > > }Date: Fri, 10 Mar 95 15:59:56 AST > }From: pamr@fritz.nsaho.ns.ca (Pamela Rhodes) > }X-Informix-List-Id: <list.5732> > } > }Is there a function in Informix 4GL SE that will convert a character into its > }ascii equivalent in integer? I want to have an array of 26 integers to count > }occurrences of letters. For instance, if I get a letter "E", I want my array > }index to be 5 and I'll add 1 to that array position. > } > }I tried: let int_var = char_var > } display int_var > } > }and got 27 for every letter of the alphabet. > > Staring with releases 4.12 and 6.00, there is a function ORD > which takes a single string argument and returns the integer > corresponding to the ASCII value of the first character of the > string. For non-string arguments, it returns 0. > > LET int_var = ORD(char_var) > > Yours, > Jonathan Leffler (johnl@informix.com) #include <disclaimer.h> For folks who don't anticipate moving to 4.12 or 6.00 soon, but still need this functionality and can use C functions, I offer the following code: /* function: ord - return numeric ASCII code for character * author: Alan Popiel * date: 27 Oct 1993 * * ord() returns the numeric value of the ASCII code for the first character * of the string passed to it by an Informix 4GL routine. * * usage: * CALL ord(str) RETURNING number * LET number = ord(str) * * CALL argument: * str -- 4GL character string, normally of length 1. If length is greater * than 1, the the code for the first character is returned. * * RETURNING argument: * integer number -- numeric value of the ASCII code; * Note: negative values are returned for ASCII codes > 127. To fix this: * IF number < 0 THEN * LET number = number + 256 * END IF */ int ord(nargs) int nargs; { char str[513]; /* Input from stack: Allow for long strings. */ /* Pop calling argument from the stack. */ popquote( str, sizeof(str) ); /* Push return argument to the stack. */ retint( (unsigned)str[0] ); /* ASCII code of first character. */ return(1); /* Number of arguments pushed. */ } I have not tested this code with non-string arguments. See the note about characters > 127. Due to the happy coincidence of names (heck, what ELSE would you name such a function?), you will be able to move your code to 4.12 or 6.00 without change. Just use the standard Informix routine in place of mine. Regards, Alan ___________________________ ______________________| R. Alan Popiel |__________________________ \\ Internet: | Martin Marietta, SLS | / \\ alan@den.mmc.com | P.O. Box 179, M/S 3810 | Std disclaimers apply. / )Voice: | Denver, CO 80201-0179 USA | ( / 303-977-9998 |___________________________| (But you knew that!) \\ /________________________) (____________________________\\