[net.unix] emulating ".ascii" with Awk

joel@bonnie.UUCP (Joel West) (01/10/86)

I need help getting awk to print out the numeric value of a character.

I've already written a csh/awk processor to convert error message
text to pre-assembled strings in assembly language (it's not
C, so don't tell me to use static char *).

However, while this works fine for BSD, where I can
	.ascii	"abcdef"
I have been unable to get it to work for System V, where I need:
	.byte	97,98,99,100,101...
Unfortunately, printing a character as "%d" with printf
gives me a "0".

What I need is one of two things:

	1) Print the numeric value of an ascii character as an
	   integer from awk(1); or
	2) Be able to pipe a string to a command (say "asbyte")
	   which has the following property:
		"asbyte abcd" prints "97,98,99,100"

I'm running awk under BSD 4.2, if there are different versions.
-- 
	Joel West
	CACI, Inc. - Federal La Jolla  (c/o Bell Labs, Whipanny)
	ihnp4!bonnie!joel

ccb@decvax.UUCP (Charles C. Bennett) (01/24/86)

Tricky, I don't think awk will do it.
Write a little shell script.

use "od -b" to break the string into bytes in octal
use "sed 's/^.......[ ]*//' to strip addresses from the od output
use tr to convert the spaces in the od output to newlines
prepend the string "ibase=8" to the stuff you have so far
feed the works to the stdin of "bc"
use tr to convert the newlines to commas
use sed to strip the telltale trailing comma

something like:

bc $TMPFILE <<!
ibase=8
`echo $STRING|od -b|sed 's/^.......[ ]*//'|tr ' ' '\012'`
!
echo `cat $TMPFILE|tr '\012' ','`|sed 's/,$//'

the final pipeline leaving the result on stdout.

A 'C' program to do this is trivial.

decvax!ccb