Last active
April 29, 2022 14:13
-
-
Save jasperck/1f04cb2248687d1a5ec0dad96f91ab1a to your computer and use it in GitHub Desktop.
Implement hex2bin and bin2hex in JavaScript
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
/** | |
* Implement hex2bin and bin2hex in JavaScript | |
* https://gist.github.com/jasperck | |
* | |
* Copyright 2017, JasperChang <[email protected]> | |
* Licensed under The MIT License | |
* http://www.opensource.org/licenses/mit-license | |
*/ | |
const hex2bin = str => str.match(/.{1,2}/g).reduce((str, hex) => str += String.fromCharCode(parseInt(hex, 16)), ''); | |
const bin2hex = str => str.split('').reduce((str, glyph) => str += glyph.charCodeAt().toString(16).length < 2 ? `0${glyph.charCodeAt().toString(16)}` | |
: glyph.charCodeAt().toString(16), ''); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Incorrect on converting! Some symbols converting wrong, not founding on system!