Summary: convert character to ASCII code? 
Author Message
 Summary: convert character to ASCII code?

I posted a query two days ago, seeking out for a way to convert
character to ASCII code.  The following is what I received from the net:

   BEGIN {
           for(i=0;i<256;i++)
               all=all sprintf("%c",i)
         }

   {
           print index(all,substr($0,1,1))
   }

   Version dependancies:  sprintf() is not in older awks, but then you mentioned
   yours had it... very old awks do not have index(), and indeed it is not
   named in my man page (although it seems to have it anyway).  SGI's "nawk"
   has both, and that is what I used to test the above. Your results may vary.

   I can't think of an elegant method (that is why I am not posting this :)
   but if you can't find any other method, you can use your sprintf to
   initialize an array in the BEGIN, where the array is:
        for (i = 32; i < 126; i++)
                array[sprintf("%c", i)] = i;
   Then you can just reference into this array to get your ascii code
   in your program -
        num = array[char];
   I haven't tested this, but I think the idea is right.

   You can try:

        echo -n "A" | od -d | awk '{print $NF}'

   This doesn't use awk exclusively, but it'll do the trick.

   I have run into this problem before and the solutions are not very
   elegant.  Here are some of the approaches I have used in the past:

    1. If the number of characters you need convert is small, ie. A-Z,
       then you can use an associative array.

       BEGIN { convert["A"]=65; convert["B"]=66 }
             { num=convert[substr($0,2,1)]; print num }

    2. Use index along with a string.

       BEGIN { convert="ABCDEFGHIJKLMNOPQRSTUVWXYZ" }
             { num=index(convert,substr($0,2,1))+64; print num }

    3. Use a loop.

       { for(num = 1; num < 128; num++)
           if(substr($0,2,1) == sprintf("%c",num))
             break
         if (num < 1 || num > 127)
           print "Cannot convert value";
         else
           print num;
       }

I think the 2nd approach is better for my current problem. that is,

BEGIN {
    for ( i = 0; i < 256; ++i )
        ASCII[ sprintf( "%c", i ) ] = i;

Quote:
}

{ print ASCII[ substr( whateverstring, whateverindex, 1 ) ]; }

The access time of associative array should be faster than the index()
function(I think).

Thanks to all of you replied.

Chih-Cherng Chin



Tue, 14 Apr 1998 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Converting Characters to Ascii code

2. Help: convert character to ASCII code?

3. Convert Character to ASCII code in 8086 Help!

4. Newbie: converting character to ascii representation

5. convert character to ascii

6. Converting STD_LOGIC_VECTOR to ASCII character

7. Converting Character to ASCII in 8086, Help! (again)

8. Converting hex digit to ASCII character

9. Converting hex digit to ASCII character

10. Convert character to ascii value

11. Converting ASCII characters <-> binary string

12. ascii-characters in source code editor clipper 5.3

 

 
Powered by phpBB® Forum Software