I have this column in a file I'm editing in VIM:
我在VIM中编辑的文件中有此列:
16128
16132
16136
16140
# etc...
And I want to convert it to this column:
我想将其转换为此列:
0x3f00
0x3f04
0x3f08
0x3f0c
# etc...
How can I do this in VIM?
我怎样才能在VIM中做到这一点?
5 个解决方案
#1
Use printf
(analogous to C's sprintf
) with the \=
command to handle the replacement:
使用printf(类似于C的sprintf)和\ =命令来处理替换:
:%s/\d\+/\=printf("0x%04x", submatch(0))
Details:
-
:%s/\d\+/
: Match one or more digits (\d\+
) on any line (:%
) and substitute (s
). -
\=
: for each match, replace with the result of the following expression: -
printf("0x%04x",
: produce a string using the format"0x%04x"
, which corresponds to a literal0x
followed by a four digit (or more) hex number, padded with zeros.
-
submatch(0)
: The result of the complete match (i.e. the number).
:%s / \ d \ + /:匹配任何行(:%)和替换(s)上的一个或多个数字(\ d \ +)。
\ =:对于每个匹配,替换为以下表达式的结果:
printf(“0x%04x”,:使用格式“0x%04x”生成一个字符串,对应于文字0x后跟四位(或更多)十六进制数字,用零填充。
submatch(0):完全匹配的结果(即数字)。
For more information, see:
有关更多信息,请参阅:
:help printf()
:help submatch()
:help sub-replace-special
:help :s
#2
Yet another way:
另一种方式:
:rubydo $_ = '0x%x' % $_
Or:
:perldo $_ = sprintf '0x%x', $_
This is a bit less typing and you avoid a level of quoting / shell escaping that you'd get if you did this via :!
. You need Perl / Ruby support compiled into your Vim.
如果您通过以下方式执行此操作,则可以减少打字/ shell转义。您需要将Perl / Ruby支持编译到您的Vim中。
#3
another way, to pass it to awk
另一种方式,将它传递给awk
awk '{printf "0x%x\n",$1}' file
#4
Select (VISUAL) the block of lines that contains the numbers, and then:
选择(VISUAL)包含数字的行块,然后:
:!perl -ne 'printf "0x\%x\n", $_'
#5
To convert a decimal into hexadecimal, use the command
要将十进制转换为十六进制,请使用该命令
:echo printf("%x", decimal)
This results in hexadecimal equivalent of decimal being displayed.
这导致显示十六进制当量小数。
Example:
:echo printf("%x", 61444)
f004 (equivalent of 61444) is displayed.
显示f004(相当于61444)。
This will not find and replace 61444 in the file.
这将无法在文件中找到并替换61444。
#1
Use printf
(analogous to C's sprintf
) with the \=
command to handle the replacement:
使用printf(类似于C的sprintf)和\ =命令来处理替换:
:%s/\d\+/\=printf("0x%04x", submatch(0))
Details:
-
:%s/\d\+/
: Match one or more digits (\d\+
) on any line (:%
) and substitute (s
). -
\=
: for each match, replace with the result of the following expression: -
printf("0x%04x",
: produce a string using the format"0x%04x"
, which corresponds to a literal0x
followed by a four digit (or more) hex number, padded with zeros.
-
submatch(0)
: The result of the complete match (i.e. the number).
:%s / \ d \ + /:匹配任何行(:%)和替换(s)上的一个或多个数字(\ d \ +)。
\ =:对于每个匹配,替换为以下表达式的结果:
printf(“0x%04x”,:使用格式“0x%04x”生成一个字符串,对应于文字0x后跟四位(或更多)十六进制数字,用零填充。
submatch(0):完全匹配的结果(即数字)。
For more information, see:
有关更多信息,请参阅:
:help printf()
:help submatch()
:help sub-replace-special
:help :s
#2
Yet another way:
另一种方式:
:rubydo $_ = '0x%x' % $_
Or:
:perldo $_ = sprintf '0x%x', $_
This is a bit less typing and you avoid a level of quoting / shell escaping that you'd get if you did this via :!
. You need Perl / Ruby support compiled into your Vim.
如果您通过以下方式执行此操作,则可以减少打字/ shell转义。您需要将Perl / Ruby支持编译到您的Vim中。
#3
another way, to pass it to awk
另一种方式,将它传递给awk
awk '{printf "0x%x\n",$1}' file
#4
Select (VISUAL) the block of lines that contains the numbers, and then:
选择(VISUAL)包含数字的行块,然后:
:!perl -ne 'printf "0x\%x\n", $_'
#5
To convert a decimal into hexadecimal, use the command
要将十进制转换为十六进制,请使用该命令
:echo printf("%x", decimal)
This results in hexadecimal equivalent of decimal being displayed.
这导致显示十六进制当量小数。
Example:
:echo printf("%x", 61444)
f004 (equivalent of 61444) is displayed.
显示f004(相当于61444)。
This will not find and replace 61444 in the file.
这将无法在文件中找到并替换61444。