如何将 convert/paste 十六进制字符串作为 vim 中的字节列表?
How to convert/paste a hex string as a list of bytes in vim?
假设我们有一个很长的十六进制字符串,形式如下:
112233..ddeeff
我想使用这个字符串来初始化容器,即 C++ 中的 std::vector。
vim 中有没有办法将其粘贴为以下形式的字节列表:
0x11, 0x22, 0x33 ... 0xdd, 0xee, 0xff
或者以其原始的十六进制字符串形式粘贴字符串,然后使用视觉选择和一些快捷方式将其快速转换为所需的字节列表形式?
Use 正则表达式:
:s/\(\x\x\)/0x, /g
在VIM中,\x
匹配一个十六进制数字。 \(
和 \)
表示我们在替换中使用 </code> 引用的子模式。添加 <em>g</em>rain of <code>g
使其应用于整行。
您可以定义一个宏来转换字符串,然后将其与键相关联。以下代码通过按 F2 将光标所在的单词转换为所需的字节列表。
function! TransformHex2byte()
" Get hex string, transform it, and delete the trailing ', '
let l:new_word = substitute( substitute( expand("<cword>") , '\x\x', "0x&, " , 'g') , ', $', '', '' )
" Change the old string with the new one.
exec "normal ciw" . new_word . "\<Esc>"
endfunction
noremap <F2> :call TransformHex2byte()<CR>
假设我们有一个很长的十六进制字符串,形式如下:
112233..ddeeff
我想使用这个字符串来初始化容器,即 C++ 中的 std::vector。 vim 中有没有办法将其粘贴为以下形式的字节列表:
0x11, 0x22, 0x33 ... 0xdd, 0xee, 0xff
或者以其原始的十六进制字符串形式粘贴字符串,然后使用视觉选择和一些快捷方式将其快速转换为所需的字节列表形式?
Use 正则表达式:
:s/\(\x\x\)/0x, /g
在VIM中,\x
匹配一个十六进制数字。 \(
和 \)
表示我们在替换中使用 </code> 引用的子模式。添加 <em>g</em>rain of <code>g
使其应用于整行。
您可以定义一个宏来转换字符串,然后将其与键相关联。以下代码通过按 F2 将光标所在的单词转换为所需的字节列表。
function! TransformHex2byte()
" Get hex string, transform it, and delete the trailing ', '
let l:new_word = substitute( substitute( expand("<cword>") , '\x\x', "0x&, " , 'g') , ', $', '', '' )
" Change the old string with the new one.
exec "normal ciw" . new_word . "\<Esc>"
endfunction
noremap <F2> :call TransformHex2byte()<CR>