Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.