1

strlen(var) can return length without leading zeros.
How to include leading zeros?
Example let var=0x00001234, width shall be 8 instead of 4.

1

In VimScript variables have (dynamic) types. After let var=0x00001234 the variable's value is a number 4660. strlen() silently converts number 4660 into a string "4660", therefore the "string length" is 4.

You can use explicit typecasts:

let varstring = "00001234"
let varnumber = str2nr(var, 16)
echo varnumber strlen(varstring) "4660 8

Also you can simply let var="0x00001234" and rely on automatic conversion, however strlen('0x00001234') is equal to 10.

0

This isn't possible with hexadecimal (or decimal) numbers, since 0x0001234 is actually just 0x1234, there's no way to "save" the leading zeros anywhere. And the variable simply cannot know that it was declared with leading zeros.

You need to just pass the variable as a string to have it know it's length with the zeros, then you can convert it to a hex number, when you're ready to use it:

let var = '00001234'
echo strlen(var)  " 8

let var = str2nr(var, 16)
" use var here
echo strlen(var)  " 4 (since var == 4660 in decimal)
  • var is a dynamic variable. strlen(var) = 4 – Fisher Jul 25 at 13:53
  • @Fisher See my edit :) – ruohola Jul 25 at 13:57

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.