8

I want to replace a character with its hex literal. For example before regex:

h

After regex:

0x68

I'm trying this:

s/\([a-z]\)/\=printf("%#02x", submatch(0))/

But this replaces it with

00

What am I doing wrong? Is this even possible?

10

In that way, printf expects a decimal number to be converted to a hexadecimal one. All you need to do is convert the character to a number as follows:

s/\([a-z]\)/\=printf("%#02x", char2nr(submatch(0)))/

See :help char2nr() for details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.