Skip to content

Commit 3b3f619

Browse files
committed
Return FALSE if non-hex char detected
Return FALSE if non-hex char detected - same behaviour as PHP version of hex2bin().
1 parent 8f760f6 commit 3b3f619

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

functions/strings/hex2bin.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ function hex2bin(s) {
55
// returns 1: 'Dima'
66
// example 2: bin2hex('00');
77
// returns 2: '\x00'
8+
// example 3: bin2hex('2f1q')
9+
// returns 3: false
810

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

1113
s += '';
1214

1315
for ( l = s.length ; i < l; i+=2 ) {
14-
ret.push(parseInt(s.substr(i, 2), 16));
16+
var c = parseInt(s.substr(i, 1), 16);
17+
var k = parseInt(s.substr(i+1, 1), 16);
18+
if(isNaN(c) || isNaN(k)) return false;
19+
ret.push( (c << 4) | k );
1520
}
1621

1722
return String.fromCharCode.apply(String, ret);

0 commit comments

Comments
 (0)