Created
January 23, 2012 22:28
-
-
Save charleskeepax/1665854 to your computer and use it in GitHub Desktop.
Vim Script Intel HEX Checksum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function IHexChecksum() | |
let l:data = getline(".") | |
let l:dlen = strlen(data) | |
if (empty(matchstr(l:data, "^:\\(\\x\\x\\)\\{5,}$"))) | |
echoerr("Input is not a valid Intel HEX line!") | |
return | |
endif | |
let l:byte = 0 | |
for l:bytepos in range(1, l:dlen-4, 2) | |
let l:byte += str2nr(strpart(l:data, l:bytepos, 2), 16) | |
endfor | |
let l:byte = (256-(l:byte%256))%256 | |
call setline(".", strpart(l:data, 0, l:dlen-2).printf("%02x", l:byte)) | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you found your way here you might want to check out the article on my blog that goes with this code: http://www.charles-keepax.co.uk/blog/IntelHEXChecksumsInVim.html