perl

How to convert char string to hex in perl (16 bit per char)


I read this post: How to convert hex to char string in perl to convert hex to chart string.

How can I do reverse operation? I need convert char string to hex in perl. For example, I have string "hello world!" and I must get:

00680065006C006C006F00200077006F0072006C00640021

Solution

  • Here's another approach. Do it all in one go with a regex.

    my $string = 'hello world!';
    $string =~ s/(.)/sprintf '%04x', ord $1/seg;