1

How to compare with hex? Wanted to test I had it right. At first I thought

let a = 13

if a == "\x0d"  | echo "Match 13" | else | echo "Not 13" |  endif " => Not 13

Then I went for

if a =~ '\%x0d' | echo "Match 13" | else | echo "Not 13" |  endif " => Not 13

As that did not work I started down a hole testing various:

'\x0d', '\%x0d', "\%x0d", '\\x0d', "\\x0d", "\\%x0d", '\xd', '\%xd', "\xd", "\%xd" ...

All with test by:

==, =~, =~#, =~?, ==#, ==?

Also tested with:

if str2nr('13')
if a+0

Always with same result "Not 13"

0

The expression you're looking for is 0x0d, which is a Number that evaluates to the hexadecimal value equivalent to 13.

if a == 0x0d  | echo "Match 13" | else | echo "Not 13" |  endif
" => Match 13

The other expressions you used don't really work because they're strings and in some of them you're using the syntax to match hexadecimal characters in patterns, which won't work either...

You can get from character "\x0d" to the number 13 with char2nr():

:echo char2nr("\x0d")
" => 13

(Note that the result is a Number, not a string.)

Conversely, you can use nr2char() to convert the 13 in a to the character with that ASCII code:

if nr2char(a) == "\x0d"  | echo "Match 13" | else | echo "Not 13" |  endif
" => Match 13

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.