To convert file with decimal to another file with Hexadecimal

I have a text file of alphanumeric values listed one by one. I have to convert them to hexadecimal equivalents for each character seperated by ":" in Unix bash shell script. For example, 12345678 has to be converted to 31:32:33:34:35:36:37:38

what is the logic for the below ?

12345678 has to be converted to 31:32:33:34:35:36:37:38

for 1 the hexadecimal is 1 only.. how come it will come as 31 ?

Hi,

Sorry! Its actually ascii charcaters to hexadecimal seperated by ":"

is it assignment ?

check here...

what you tried so far ?

This should do it..

 
echo "1234" | perl -F// -lane 'BEGIN{$\=":"} print "", unpack("H*",$_) for @F'

This is the output I've got

 
31:32:33:34:

Its not an assignment.

I went to the link u suggested above. It talks about ascii chart and extended ascii chart (which i already have), hence not useful for me.

There is another link talks about file. It refers to some order.txt but that order.txt is not available.

and read how to read character by character

and you can easily form a solution

1 Like

@getmmg,

Thanks, it works for conversion. But this does not seem to print line by line. In my file, the string in each line is 19 characters. Also, if i can get the output in a file, it will be more useful

For file, i tried:

for i in `cat xyz.txt`; do
echo "$i" | perl -F// -lane 'BEGIN{$\=":"} print "", unpack("H*",$_) for @F'
done

This should print them line by line.

perl -F// -lane 'BEGIN{$"=":"} push(@arr,unpack("H*",$_)) for @F; print "@arr"; @arr=()' ascii

If you want the o/p in a file, Redirect it using >.

1 Like

Assuming 19 characters to dump per line and that the newline is a delimiter which should be passed through unmodified (tested with OSX's hexdump):

hexdump -ve '18/1 "%x:" /1 "%x" /1 "%c"' infile > outfile

Regards,
Alister

1 Like

@getmmg,

Thanks for the help. It worked.

@itkamaraj,

Thanks for your pointers! They helped me to write my own code in unix shell script.

@alister,

Thank you!

---------- Post updated at 12:20 PM ---------- Previous update was at 12:15 PM ----------

My query is resolved and this thread can be closed!