For Programmers: Free Programming Magazines  


Home > Archive > AWK > July 2006 > Can we convert a char to ascii in awk









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 Can we convert a char to ascii in awk
blacky

2006-07-10, 6:56 pm

Hi all,
Is there any way to convert a given CHAR to its ASCII value.. We can
convert an ASCII value to its corresponding character.. How could we
get the reverse case..

Thanks,
Thillai.

Kenny McCormack

2006-07-10, 6:56 pm

In article <1152550626.509577.148530@p79g2000cwp.googlegroups.com>,
blacky <thillai.abirami@gmail.com> wrote:
>Hi all,
> Is there any way to convert a given CHAR to its ASCII value.. We can
>convert an ASCII value to its corresponding character.. How could we
>get the reverse case..


There is an ugly, but "portable" way that others will no doubt chime in
with soon, but I have to ask you what is your platform and version of AWK.

This is because the most likely answers are Linux and/or gawk, and if it
*is* gawk, then there are easier ways. And if it is TAWK (lucky you!),
then there is a built-in for it.

blacky

2006-07-11, 6:56 pm

Ya,

The version i use here is gawk only..
Can you tell me the ways..
Thanks,
Thillai




Kenny McCormack wrote:
> In article <1152550626.509577.148530@p79g2000cwp.googlegroups.com>,
> blacky <thillai.abirami@gmail.com> wrote:
>
> There is an ugly, but "portable" way that others will no doubt chime in
> with soon, but I have to ask you what is your platform and version of AWK.
>
> This is because the most likely answers are Linux and/or gawk, and if it
> *is* gawk, then there are easier ways. And if it is TAWK (lucky you!),
> then there is a built-in for it.


Andrew Schorr

2006-07-11, 6:56 pm

blacky wrote:
> Ya,
>
> The version i use here is gawk only..
> Can you tell me the ways..


If you can load xgawk on your system, then it is very easy. For
example:

bash-3.00$ xgawk -lordchr 'BEGIN {x = "A"; print ord(x); print
chr(ord(x))}'
65
A

The source to xgawk can be downloaded here:
http://sourceforge.net/projects/xmlgawk

Regards,
Andy

Kenny McCormack

2006-07-11, 6:56 pm

In article <1152624985.495263.15260@m73g2000cwd.googlegroups.com>,
Andrew Schorr <aschorr@telemetry-investments.com> wrote:
>blacky wrote:
>
>If you can load xgawk on your system, then it is very easy. For
>example:
>
>bash-3.00$ xgawk -lordchr 'BEGIN {x = "A"; print ord(x); print
>chr(ord(x))}'
>65
>A
>
>The source to xgawk can be downloaded here:
>http://sourceforge.net/projects/xmlgawk


I'll have to take a look at x[ml]gawk sometime.

Anyway, in basic gawk, I believe the current shipping version includes
an extension lib for this. You will have to work out the details of
getting the extension lib compiled, installed, and accessed from your
GAWK script. XGAWK just makes this all a lot more seamless (AIUI).

Chris F.A. Johnson

2006-07-11, 6:56 pm

On 2006-07-10, blacky wrote:
> Hi all,
> Is there any way to convert a given CHAR to its ASCII value.. We can
> convert an ASCII value to its corresponding character.. How could we
> get the reverse case..


If you have a unix shell available:

awk -v SQ="'" '{
cmd="printf \"%d\\n\" \"" SQ $1 "\""
system( cmd )
}'

In the shell itself:

printf "%s\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
John DuBois

2006-07-12, 3:56 am

In article <1152550626.509577.148530@p79g2000cwp.googlegroups.com>,
blacky <thillai.abirami@gmail.com> wrote:
> Is there any way to convert a given CHAR to its ASCII value.. We can
>convert an ASCII value to its corresponding character.. How could we
>get the reverse case..


For occasional use:

function asc(c,
tchar, ascval, b) {
if (c == "")
return ""
c = substr(c, 1, 1)
ascval = b = 128
while ((tchar = sprintf("%c", ascval)) != c)
ascval += (b /= 2) * (tchar < c) ? 1 : -1
return int(ascval)
}

If you're going to be doing it a lot, it's worth filling in a char->ascii-value
table.

John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
Stephan Titard

2006-07-12, 7:56 am

John DuBois escribió:
> In article <1152550626.509577.148530@p79g2000cwp.googlegroups.com>,
> blacky <thillai.abirami@gmail.com> wrote:
>
> For occasional use:
>
> function asc(c,
> tchar, ascval, b) {
> if (c == "")
> return ""
> c = substr(c, 1, 1)
> ascval = b = 128
> while ((tchar = sprintf("%c", ascval)) != c)
> ascval += (b /= 2) * (tchar < c) ? 1 : -1
> return int(ascval)
> }
>
> If you're going to be doing it a lot, it's worth filling in a char->ascii-value
> table.
>
> John

or use memoization:
build the table as you call the function
give back the table value if already calculated...

maybe it is even possible to write a memoize package


hth
--stephan
quarkLore

2006-07-22, 9:56 pm

You need char to ascii conversion in gawk, can't it be acheived using
printf("%d",c) ?

Am I missing some point? Clarifications please

Patrick TJ McPhee

2006-07-23, 6:56 pm

In article <1153624386.543375.139930@h48g2000cwc.googlegroups.com>,
quarkLore <agarwal.prat@gmail.com> wrote:

% You need char to ascii conversion in gawk, can't it be acheived using
% printf("%d",c) ?

No. Suppose c is "a". awk will see that it needs a number, convert "a"
to a number, and come up with 0.


--

Patrick TJ McPhee
North York Canada
ptjm@interlog.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com