Unix Shell - Re: [missing the obvious?] ascii->int

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > July 2005 > Re: [missing the obvious?] ascii->int





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Re: [missing the obvious?] ascii->int
Chris F.A. Johnson

2005-07-29, 6:01 pm


[follow-up set to comp.unix.shell]

On 2005-07-29, giacomo boffi wrote:
> ok, i'm probably missing the obvious, but i'd like to write a program
> to convert ascii to integer:
> ,----
>| % echo A | awk -f ascii2int
>| 65
>| %
> `----
>
> and i cannot figure out the appropriate trick...


Unless there's a particular reason to use awk, I'd use the shell:

asc() {
printf "%d\n" "'$1"
}

> ps: oh, what i really need is a "aseq" program:
> ,----
>| % echo e | awk -f aseq
>| a
>| b
>| c
>| d
>| e
>| % echo D | awk -f aseq
>| A
>| B
>| C
>| D
>| %
> `----
>
> but i think that's in my reach, if i know how to do step 1 ;


Again, the shell is easy enough (and you do not need step 1):

aseq() {
alpha='A B C D E F G H I J K L M N O P Q R S T U V W X Y Z'
printf "%s\n" ${alpha%$1*} $1
}


Even easier in bash3:

aseq() {
eval "printf '%s\n' {A..$1}"
}


--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com