使用m4将字符串转换为ASCII代码点

这应该是可能的,但由于我是m4的新手,我不知道如何去做,或者如何写一个算法来做到这一点(在m4中)。

编辑:

刚解决它,无论如何将来参考,我有一系列字符,它们需要被翻译成等效的ASCII码点,例如

ascii(-{COLON}-, -{:}-) => #define TKN_COLON 58 

为了对纯m4实现感兴趣的其他人的利益,我设法创建了以下转换宏。 它必须插入引号字符以支持正常的m4引用字符。 如果这不重要,可以简化一下。

 changequote() # Change quotes so we can handle "`" and "'" # Change quotes every time this macro is called define(,)_asciiord()changequote`'dnl !>) # Convert chars one at a time in the string argument define(,,,, dnl ,0,1))ifelse(len(),1,,) $0(substr(,1))!>)!>) # Map ASCII chars to their integer index by position # Control chars are not supported. # If the comment character is changed you must alter the map accordingly define(,,,32,,,35,dnl ?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ !>,)!>)!>) changequote # Restore normal quoting chars 

用法:

 asciiord(`hello') --> 104, 101, 108, 108, 111 

我用以下代码部分完成了这个

 divert(-1) changequote(-{,}-) changecom(/*,*/) define(-{ascii}-,-{#define TKN_-{}-$1 esyscmd(-{python -c "import sys; sys.stdout.write('%d'%ord('$2'))"}-)}-) divert(0)dnl 

如果有人知道更好的方法(例如本机m4或更多便携式shell命令),我将不胜感激。