Skip to content

Instantly share code, notes, and snippets.

@Janaka-Steph
Created October 30, 2018 12:37
Show Gist options
  • Save Janaka-Steph/46f010718fe527d103fe7e0bdb9dcd0a to your computer and use it in GitHub Desktop.
Save Janaka-Steph/46f010718fe527d103fe7e0bdb9dcd0a to your computer and use it in GitHub Desktop.
Convert From/To Binary/Decimal/Hexadecimal in JavaScript + calculate byte length
const convert = {
bin2dec : s => parseInt(s, 2).toString(10),
bin2hex : s => parseInt(s, 2).toString(16),
dec2bin : s => parseInt(s, 10).toString(2),
dec2hex : s => parseInt(s, 10).toString(16),
hex2bin : s => parseInt(s, 16).toString(2),
hex2dec : s => parseInt(s, 16).toString(10),
lenBytesDec: bin => (bin.length/8).toString(10),
lenBytesHex: bin => (bin.length/8).toString(16),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment