-
-
Save lanrion/ce66c0f7e71581e007b768fd27564b3f to your computer and use it in GitHub Desktop.
Hex string to byte and other way round conversion functions.
This file contains hidden or 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
function byteToHexString(uint8arr) { | |
if (!uint8arr) { | |
return ''; | |
} | |
var hexStr = ''; | |
for (var i = 0; i < uint8arr.length; i++) { | |
var hex = (uint8arr[i] & 0xff).toString(16); | |
hex = (hex.length === 1) ? '0' + hex : hex; | |
hexStr += hex; | |
} | |
return hexStr.toUpperCase(); | |
} | |
function hexStringToByte(str) { | |
if (!str) { | |
return new Uint8Array(); | |
} | |
var a = []; | |
for (var i = 0, len = str.length; i < len; i+=2) { | |
a.push(parseInt(str.substr(i,2),16)); | |
} | |
return new Uint8Array(a); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment