Skip to content

Commit d94f3cc

Browse files
committed
Merge pull request locutusjs#194 from duzun/master
Added hex2bin.js
2 parents 4b349ff + 824de6a commit d94f3cc

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

functions/strings/hex2bin.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function hex2bin(s) {
2+
// discuss at: http://phpjs.org/functions/hex2bin/
3+
// original by: Dumitru Uzun (http://duzun.me)
4+
// example 1: hex2bin('44696d61');
5+
// returns 1: 'Dima'
6+
// example 2: hex2bin('00');
7+
// returns 2: '\x00'
8+
// example 3: hex2bin('2f1q')
9+
// returns 3: false
10+
11+
var ret = [], i = 0, l;
12+
13+
s += '';
14+
15+
for ( l = s.length ; i < l; i+=2 ) {
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 );
20+
}
21+
22+
return String.fromCharCode.apply(String, ret);
23+
}

0 commit comments

Comments
 (0)