ord.awk 937 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # ord.awk --- do ord and chr
  2. # Global identifiers:
  3. # _ord_: numerical values indexed by characters
  4. # _ord_init: function to initialize _ord_
  5. #
  6. # Arnold Robbins, arnold@skeeve.com, Public Domain
  7. # 16 January, 1992
  8. # 20 July, 1992, revised
  9. BEGIN { _ord_init() }
  10. function _ord_init( low, high, i, t)
  11. {
  12. low = sprintf("%c", 7) # BEL is ascii 7
  13. if (low == "\a") { # regular ascii
  14. low = 0
  15. high = 127
  16. } else if (sprintf("%c", 128 + 7) == "\a") {
  17. # ascii, mark parity
  18. low = 128
  19. high = 255
  20. } else { # ebcdic(!)
  21. low = 0
  22. high = 255
  23. }
  24. for (i = low; i <= high; i++) {
  25. t = sprintf("%c", i)
  26. _ord_[t] = i
  27. }
  28. }
  29. function ord(str, c)
  30. {
  31. # only first character is of interest
  32. c = substr(str, 1, 1)
  33. return _ord_[c]
  34. }
  35. function chr(c)
  36. {
  37. # force c to be numeric by adding 0
  38. return sprintf("%c", c + 0)
  39. }