Can we convert a char to ascii in awk

  • Follow


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.

0
Reply thillai.abirami (1) 7/10/2006 4:57:06 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.

0
Reply gazelle 7/10/2006 5:16:25 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:
> >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.

0
Reply blacky 7/11/2006 1:17:01 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

0
Reply Andrew 7/11/2006 1:36:25 PM

In article <1152624985.495263.15260@m73g2000cwd.googlegroups.com>,
Andrew Schorr <aschorr@telemetry-investments.com> wrote:
>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

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).

0
Reply gazelle 7/11/2006 1:48:51 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
0
Reply Chris 7/11/2006 3:01:24 PM

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/
0
Reply spcecdt 7/12/2006 3:43:28 AM

John DuBois escribi�:
> 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
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
0
Reply Stephan 7/12/2006 10:22:00 AM

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

Am I missing some point? Clarifications please

0
Reply quarkLore 7/23/2006 3:13:06 AM

In article <1153624386.543375.139930@h48g2000cwc.googlegroups.com>,
quarkLore <agarwal.prateek@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
0
Reply ptjm 7/23/2006 11:02:19 PM

9 Replies
734 Views

(page loaded in 0.159 seconds)

Similiar Articles:













7/22/2012 12:04:27 PM


Reply: