Javascript bin2hex : Strings : PHP functions in JavaScript JAVASCRIPT TUTORIALS


JAVASCRIPT TUTORIALS » PHP functions in JavaScript » Strings »

 

Javascript bin2hex


Converts the binary representation of data to hex
Example 1

Running
1.bin2hex('Kev');

Could return
1.'4b6576'
Example 2

Running
1.bin2hex(String.fromCharCode(0x00));

Could return
1.'00'
function bin2hex(s){
    // Converts the binary representation of data to hex  
    // 
    // version: 812.316
    // discuss at: http://phpjs.org/functions/bin2hex
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   bugfixed by: Linuxworld
    // *     example 1: bin2hex('Kev');
    // *     returns 1: '4b6576'
    // *     example 2: bin2hex(String.fromCharCode(0x00));
    // *     returns 2: '00'
    var v,i, f = 0, a = [];
    s += '';
    f = s.length;
    
    for (i = 0; i<f; i++) {
        a[i] = s.charCodeAt(i).toString(16).replace(/^([\da-f])$/,"0$1");
    }
    
    return a.join('');
}


HTML code for linking to this page:

Follow Navioo On Twitter

JAVASCRIPT TUTORIALS

 Navioo PHP functions in JavaScript
» Strings