07-17-06 12:20 AM
On 2006-07-16, Dave Farrance wrote:
> The "Advanced Bash-Scripting Guide" has a section on string manipulation
> that's useful. I can find the equivalent of most of the BASIC string
> functions that I used a quarter of a century ago.
>
> I've now figured out (with a little help from this group) that the Bash
> equivalent of the BASIC CHR$() function for converting a decimal number
> into its ASCII character is:
>
> $(printf "\\$(printf "%03o" $x)")
>
> Can Bash go the other way - from a character to its ASCII decimal number
> - like the BASIC ASC() function?
From the SUS printf spec:
* If the leading character is a single-quote or double-quote, the
value shall be the numeric value in the underlying codeset of
the character following the single-quote or double-quote.
E.g.:
CHAR=A
printf "%d\n" "'$CHAR"
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
[ Post a follow-up to this message ]
|