Search the web
Sign In
New User? Sign Up
vim · Vim (Vi IMproved) text editor users list
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want your group to be featured on the Yahoo! Groups website? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Click here for the latest updates on Groups Message search

Messages

  Messages Help
Advanced
Submatch question in a substitute   Message List  
Reply Message #105714 of 113126 |

Hi,

When I execute this line

:%s/\<[01]\+\>/\=Bin2Hex(submatch(0))/

on this binary tabel

0000
0001
[...]
1111

it is all converted nicely to hexadecimal

However, my binary numbers are prefixed with a % character.
So
%0000
%0001
[...]
%1111

How to I change the substitute command above so that it works again?
I tried
:%s/\<%[01]\+\>/\=Bin2Hex(submatch(1))/
But this one gives an pattern not found error

And
:%s/%\<[01]\+\>/\=Bin2Hex(submatch(1))/
changes everything into zero.
So I assume the submatch position is wrong


Here below the Bin2Hex function is described.
Note: Not developed by my but provided trough this fantastic mailing group
+-------------------------------------------------------------------------------\
-------------------------+
func! Bin2Hex(bin)
let bin = matchstr(a:bin, '[01]\+')
if bin == ""
return "0"
endif
let bin = repeat("0", PadSize(strlen(bin), 4)). bin
let runs = split(bin, '....\zs')
call map(runs, 's:fourbd2hd(v:val)')
return join(runs, '')
endfunc

" return the first run of hex digits in string {hex} converted into
" binary digits (a match at "0x" is skipped)
func! Hex2Bin(hex)
let hex = matchstr(a:hex, '\%(0x\)\@!\x\+')
if hex == ""
return "0"
endif
return join(map(split(hex, '\m'), 's:hd2fourbd(v:val)'), '')
endfunc

" convert 4 bin digits (string) into 1 (lower case) hex digit
func! s:fourbd2hd(bd)
let bd = a:bd
return printf("%x", 8*bd[0] + 4*bd[1] + 2*bd[2] + bd[3])
endfunc

" convert 1 hex digit (string) into 4 bin digits
func! s:hd2fourbd(hd)
let hd = eval("0x". a:hd)
return (hd/8) . (hd/4%2) . (hd/2%2) . (hd%2)
endfunc

" calculate number of pad chars to make {len} a multiple of {chunksize}
func! PadSize(len, chunksize)
return -1 + (a:chunksize - (a:len + a:chunksize - 1) % a:chunksize)
endfunc
" padsize := PadSize(len,chunksize) <=>
" 0 <= padsize < chunksize AND (len + padsize) mod chunksize = 0

" NOTE: must not use substitute() in the functions to avoid recursive
" submatch() when called from :s/.../\=.../
" substitute(bin,'[01]\{4}', '\=s:fourbd2hd(submatch(0))', 'g')
" substitute(a:hex, '\x', '\=s:hd2fourbd(submatch(0))', 'g')
+-------------------------------------------------------------------------------\
-------------------------+

Rgds,
Jeri

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---




Thu Jul 9, 2009 7:35 pm

jeri.raye@...
Send Email Send Email

Message #105714 of 113126 |
Expand Messages Author Sort by Date

Hi, When I execute this line ... on this binary tabel 0000 0001 [...] 1111 it is all converted nicely to hexadecimal However, my binary numbers are prefixed...
Jeri Raye
jeri.raye@...
Send Email
Jul 9, 2009
7:35 pm

... The \< won't match on the left of a "%" (unless the "%" is in your 'isk' setting: ":help 'isk' for more on that"). Move your "%" to the other side of the...
Tim Chase
vim@...
Send Email
Jul 9, 2009
8:00 pm
Advanced

Copyright © 2010 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help