Skip to content

Instantly share code, notes, and snippets.

@charleskeepax
Created January 23, 2012 22:28
Show Gist options
  • Save charleskeepax/1665854 to your computer and use it in GitHub Desktop.
Save charleskeepax/1665854 to your computer and use it in GitHub Desktop.
Vim Script Intel HEX Checksum
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
@charleskeepax
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment