Perl hex string for binary string

I am trying to convert a string of hexadecimal digits to a binary string. If my input line is 41424344, then I want the line to save "ABCD". How can I do that?

+6
source share
3 answers

You can do this without using a regular expression with pack :

 print pack 'H*', '41424344'; 

Conclusion:

 ABCD 
+13
source

Canonical method

 $input_string =~ s/(..)/chr(hex($1))/ge; 

This reads two characters at a time from the input, calling hex (converting the hexadecimal number to decimal number), and then chr (converting the decimal number to character) on each input.

+1
source
 s/([a-f0-9][a-f0-9])/chr(hex($1))/egi; 
+1
source

Source: https://habr.com/ru/post/911687/


All Articles