From: moonhkt on
Hi All

How to dump the corresponding Hex value for each character using od or
other unix command ? Print original character and the hex value.

Data as below

A
B
C
1
2
3
From: Vladimir Usenko on
Try use `hexdump'
admin(a)phoenix:/home/admin>printf "A" | hexdump -x -C
0000000 0041
00000000 41 |A|
00000001


On 01/28/10 16:46, moonhkt wrote
> Hi All
>
> How to dump the corresponding Hex value for each character using od or
> other unix command ? Print original character and the hex value.
>
> Data as below
>
> A
> B
> C
> 1
> 2
> 3


--
UV-RIPE
From: jellybean stonerfish on
On Thu, 28 Jan 2010 06:46:18 -0800, moonhkt wrote:

> Hi All
>
> How to dump the corresponding Hex value for each character using od or
> other unix command ? Print original character and the hex value.
>
> Data as below
>
> A
> B
> C
> 1
> 2
> 3

I am an eternal noob, so this may be over complicated or over simplified.

# make a loop putting one character in CHAR for each iteration
for CHAR in A B C 1 2 3;
# echo the character followed by a space, without a newline
do echo -n "$CHAR "
# echo the character again, and pipe it to xxd
# the -p option to xxd give plain output
echo -n $CHAR | xxd -p;
# end loop
done
From: Ed Morton on
On Jan 28, 8:46 am, moonhkt <moon...(a)gmail.com> wrote:
> Hi All
>
> How to dump the corresponding Hex value for each character using od or
> other unix command ? Print original character and the hex value.
>
> Data as below
>
> A
> B
> C
> 1
> 2
> 3

$ cat file
A
B
C
1
2
3

$ gawk '{print $0,strtonum("0x"$0)}' file
A 10
B 11
C 12
1 1
2 2
3 3

$ gawk --non-decimal-data '{printf "%s %d\n",$0,"0x"$0}' file
A 10
B 11
C 12
1 1
2 2
3 3

$ while read c; do printf "%s %d\n" "$c" "0x$c"; done < file
A 10
B 11
C 12
1 1
2 2
3 3

Note that both --non-decimal-data and strtonum() are GNU awk
extensions and that final example was in bash.

Ed.
From: Stephane CHAZELAS on
2010-01-28, 06:46(-08), moonhkt:
[...]
> How to dump the corresponding Hex value for each character using od or
> other unix command ? Print original character and the hex value.
>
> Data as below
>
> A
> B
> C
> 1
> 2
> 3

awk '
BEGIN{for (i=0;i<256;i++) x[sprintf("%c",i)]=sprintf("%x",i)}
{print $0, x[$0]}'

Or:

perl '-lpe$_=join$",unpack"aXH2",$_'

--
St�phane