Skip to content

Instantly share code, notes, and snippets.

@jasperck
Last active April 29, 2022 14:13
Show Gist options
  • Save jasperck/1f04cb2248687d1a5ec0dad96f91ab1a to your computer and use it in GitHub Desktop.
Save jasperck/1f04cb2248687d1a5ec0dad96f91ab1a to your computer and use it in GitHub Desktop.
Implement hex2bin and bin2hex in JavaScript
/**
* Implement hex2bin and bin2hex in JavaScript
* https://gist.github.com/jasperck
*
* Copyright 2017, JasperChang <jasperc8@gmail.com>
* 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), '');
@issdilshod
Copy link

issdilshod commented Apr 29, 2022

Incorrect on converting! Some symbols converting wrong, not founding on system!
image

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