awk equivalent for casting char to int? 
Author Message
 awk equivalent for casting char to int?

I would like to convert a single-character string into it's ASCII value.
Is this possible with AWK?

For my particular problem, I already have a solution. Because I only
need a subset of ASCII, I can use a string like
   "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]" (...etcetera...)
and use index().

But what if I would like to convert, for example, any character to an int?



Tue, 15 Dec 1998 03:00:00 GMT  
 awk equivalent for casting char to int?


Quote:
> I would like to convert a single-character string into it's ASCII value.
> Is this possible with AWK?

For number -> character we have sprintf ( "%c" , n )

For character -> number, this kluge is what I have always used:

BEGIN {
  # Make this string for looking up all ascii characters (1-255):
  C = "" ; for ( i = 0 ; ++i < 256 ; ) C = C sprintf ( "%c" , i )

Quote:
}

# Then whenever you want the numeric value of a character, use:

       num = index ( C , char )

Null characters don't exist in strings, but if they did,
a correct result of zero would be obtained.

-----------------------------------------------------------



Tue, 15 Dec 1998 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Equivalent to c/c++'s (int argc, char *argv[])

2. (/= (char-int c) (char-code c))?

3. Newbie: Casting Int to string....

4. Char data type equivalent

5. char ** equivalent

6. Tcl equivalent to C++ ofstream::attach(int)

7. How do i combine a [Char] with an [Int]

8. char -> int

9. Int 10H func 0AH - bit pattern of chars?

10. int -> char

11. simple int to char question

12. simple int to char question

 

 
Powered by phpBB® Forum Software