Skip to content

Commit

Permalink
Return FALSE if non-hex char detected
Browse files Browse the repository at this point in the history
Return FALSE if non-hex char detected - same behaviour as PHP version of hex2bin().
  • Loading branch information
duzun committed Oct 25, 2014
1 parent 8f760f6 commit 3b3f619
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion functions/strings/hex2bin.js
Expand Up @@ -5,13 +5,18 @@ function hex2bin(s) {
// returns 1: 'Dima'
// example 2: bin2hex('00');
// returns 2: '\x00'
// example 3: bin2hex('2f1q')
// returns 3: false

var ret = [], i = 0, l;

s += '';

for ( l = s.length ; i < l; i+=2 ) {
ret.push(parseInt(s.substr(i, 2), 16));
var c = parseInt(s.substr(i, 1), 16);
var k = parseInt(s.substr(i+1, 1), 16);
if(isNaN(c) || isNaN(k)) return false;
ret.push( (c << 4) | k );
}

return String.fromCharCode.apply(String, ret);
Expand Down

0 comments on commit 3b3f619

Please sign in to comment.