Permalink
Browse files

bin2hex improved by ntoniazzi

  • Loading branch information...
1 parent 0a4afa4 commit fe540ca0f15a4127204ba7615f64fcaafb33c81f @kukawski kukawski committed Jun 29, 2012
Showing with 10 additions and 9 deletions.
  1. +10 −9 functions/strings/bin2hex.js
@@ -3,19 +3,20 @@ function bin2hex (s) {
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Onno Marsman
// + bugfixed by: Linuxworld
+ // + improved by: ntoniazzi (http://phpjs.org/functions/bin2hex:361#comment_177616)
// * example 1: bin2hex('Kev');
// * returns 1: '4b6576'
// * example 2: bin2hex(String.fromCharCode(0x00));
// * returns 2: '00'
- var i, f = 0,
- a = [];
+
+ var i, l, o = "", n;
- s += '';
- f = s.length;
-
- for (i = 0; i < f; i++) {
- a[i] = s.charCodeAt(i).toString(16).replace(/^([\da-f])$/, "0$1");
+ s += "";
+
+ for (i = 0, l = s.length; i < l; i++) {
+ n = s.charCodeAt(i).toString(16)
+ o += n.length < 2 ? "0" + n : n;
}
-
- return a.join('');
+
+ return o;
}

0 comments on commit fe540ca

Please sign in to comment.