J
Jeffrey Ross
Hi.
I can convert "414243" to "ABC" using awk (see below) but wonder if there's
a simpler way of doing it in Perl.
perl -e 'printf "%c", hex("41")' # works for a single
hard-coded 2-character string
How do I do it for lines of arbitrary length?
My awk equivalent is:
awk 'BEGIN {for (i=0;i<=15;i++){
for (j=0;j<=15;j++){a[sprintf("%x%x",i,j)]=i*16+j}
}
}
{
for (i=1;i<=length($0);i+=2) {
x=tolower(substr($0,i,1))
y=tolower(substr($0,i+1,1))
printf "%c",a[x y]
}
print ""
}'
TIA.
Jeffrey
I can convert "414243" to "ABC" using awk (see below) but wonder if there's
a simpler way of doing it in Perl.
perl -e 'printf "%c", hex("41")' # works for a single
hard-coded 2-character string
How do I do it for lines of arbitrary length?
My awk equivalent is:
awk 'BEGIN {for (i=0;i<=15;i++){
for (j=0;j<=15;j++){a[sprintf("%x%x",i,j)]=i*16+j}
}
}
{
for (i=1;i<=length($0);i+=2) {
x=tolower(substr($0,i,1))
y=tolower(substr($0,i+1,1))
printf "%c",a[x y]
}
print ""
}'
TIA.
Jeffrey