Skip to content

Instantly share code, notes, and snippets.

@Higgs1
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Higgs1/d828714f8484390529cf to your computer and use it in GitHub Desktop.
Save Higgs1/d828714f8484390529cf to your computer and use it in GitHub Desktop.
CoffeeScript String ⇔ Hex
str2hex = do ->
hex = ['0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
hex = (hex[x >> 4] + hex[x & 15] for x in [0..255])
(str) ->
(hex[c.charCodeAt()] for c in str).join ''
hex2str = (hex) ->
(String.fromCharCode parseInt hex[i...i + 2], 16 for i in [0...hex.length] by 2).join ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment